lec 2
DESCRIPTION
TRANSCRIPT
BITS Pilani, Pilani Campus
BITS PilaniPilani Campus
Dr. Yashvardhan SharmaCSIS Dept., BITS-Pilani
IS F213 Object Oriented Programming
BITS Pilani, Pilani Campus
Today’s Agenda
• Compiling/Executing Java Programs (Review)• Object Basics• Three Pillars of OOP• Basic Java Syntax
• Java Type System
• Differences Between C & Java
• System.out.println() and System.out.print() methods
BITS Pilani, Pilani Campus
Java Features
• Compiled and Interpreted• Platform Independent• Architectural Neutral
BITS Pilani, Pilani Campus
Java Program Structure
Documentation Section1. /**…… */ Documentation Comments2. // Single Line Comment3. /*…… */ Multi line Comments
Package Statement
Import Statements
Interface Statements
Class Definitions
Main Method class
BITS Pilani, Pilani Campus
Compiling/Executing a Java Application
Source Code
Java Compiler
ByteCode
<< .java file>>
<< javac .java >>
<< .class file>>
Java Interpreter
Machine Code
<< java [name of class] >>
BITS Pilani, Pilani Campus
Example 1
class Test1{public static void main(String[] x){System.out.println("Hello Java");}} D:\programs>javac yash.java
D:\programs>java Test1Hello Java
D:\program>java test1Exception in thread "main" java.lang.NoClassDefFoundError: test1 (wrong name: Test1)
Name of Source File is
yash.java
BITS Pilani, Pilani Campus
Example 2
// oop.javaclass A{public static void main(String args[]){System.out.println("This is class A");}}class B{public static void main(String args[]){System.out.println("This is class B");}}
D:\programs>javac oop.java
D:\programs>java oopException in thread "main" java.lang.NoClassDefFoundError: oop
D:\programs>java AThis is class A
D:\programs>java BThis is class B
BITS Pilani, Pilani Campus
What’s Object
• Term Object Means Combination of Data (Attributes) and Logic (Behavior, Functions, Operations) of some real world entity
• Every object has two parts :- Data Part [ Instance Fields in Java, Attributes or properties]- Operations Part [ methods in java, Behavior]
• Examples :1. Account :
Attributes : Account Holder, Account type [saving , current] , Balance Operations : deposit, withdraw,
2. BOX: Attributes : length, width, height, color Operations : compute area, compute volume
Data
Methods Methods
Methods Methods
Object keeps Data Part + Operation Part Together
BITS Pilani, Pilani Campus
What’s class
• Objects are grouped in classes• Class collections of objects having similar
behavior and properties• A single object is simply an instance of class.• Objects can not be instantiated (or created)
without defining a class• Classes are defined whereas objects are
created.
BITS Pilani, Pilani Campus
Object State
• Properties/Attribute Values at a particular moment represents the state of an object
• Object may or may not change state in response to an outside stimuli. • Objects whose state can not be altered after creation are known as immutable
objects and class as immutable class [ String class in Java]• Objects whose state can be altered after creation are known as mutable objects
and class as mutable class [ StringBuffer class in Java]
States of Three Different INSTANCES of CAR CLASS
MARUTI800
Name : maruti800Price : 200789Color : Red
MarutiESTEEM
Name : marutiEsteemPrice : 500789Color : whilte
MARUTIZEN
Name : marutiZenPrice : 200789Color : metallic white
BITS Pilani, Pilani Campus
Data Abstraction
• Way of managing complexity• Abstraction refers to the act of representing
essential features that are of interest of the users without including background details or explanation
• Classes use the concept of encapsulation for hiding unnecessary implementation details
BITS Pilani, Pilani Campus
Pillars of OOP
• Encapsulation• Inheritance• Polymorphism
BITS Pilani, Pilani Campus
Encapsulation
• Encapsulation means wrapping up of data and methods (operations , code) together
• Access to code and data is tightly controlled. • Through we can define what and what can not be
accessible outside. [ public , private , protected ]
Data
Methods Methods
Methods Methods
Encapsulation keeps Data Part + Operation Part Together inside a capsule
BITS Pilani, Pilani Campus
Encapsulation Example
class BOX
{
private double length;
private double width;
private double height;
public double area() { }
public double volume() { }
} // End of class BOX
lengthwidthheight
area()
volume()
BOX Class
BITS Pilani, Pilani Campus
Inheritance
• Process by which one object acquires properties of other classes• Supports Reusability of code and data• The class whose properties are extended is known as super or base or
parent class.• The class which extends the properties of super class is known as sub
or derived or child class • In Java a class can either extends another class or can implement an
interface
A
B <<class>>
<<interface>>A
B
<<class>>
<<class>>
class B extends A class B implements A
BITS Pilani, Pilani Campus
Various Forms of Inheritance
A
B
Single Inheritance
A
B
Hierarchical Inheritance
X
A B C
X
A B C
MultiLevel Inheritance
A
B
C
A
B
C
A B
C
Multiple InheritanceNOT SUPPORTED BY JAVA
A B
C
SUPPORTED BY JAVA
BITS Pilani, Pilani Campus
Polymorphism
• Poly means many and morph means forms. • One Interface Many forms• One Interface for several general class of actions• In Java we have two types of polymorphisms
1. Compile-Time [ Method Overloading]2. Run-Time [ Method Overriding]
BITS Pilani, Pilani Campus
Method OverLoading
• Two methods are said to be overloaded if they have same name and different signatures.
• Signature of method means number of arguments to method and their types.
• Two methods are said to have different signatures if they differ in number of arguments they receive or their types
• Example Signature
1. int sum (int a, int b)
2. float sum (int a, float b)
3. double sum(double a , double b)
4. void draw();
sum(int,int)
sum(int,float)
sum(double,double)
draw()
Note : return type and name of arguments are not part of signatures
Signatures
BITS Pilani, Pilani Campus
Method Overloading Examples
1. void sum(int a,int b)
2. int sum(float a, float b)
3. double sum (double a , double b)
4. int sum(int a, float b)
5. float sum(float a,int b)
Overloaded Methods
1. void sum(int a,int b)
2. int sum(int x, int y)
3. double sum (double a , double b)
4. float sum(double a1, double b)
1,2 and 3,4 Not Overloaded
1,3 and 2,4 are overloaded
BITS Pilani, Pilani Campus
Java Type System
• Type specifies set values + set of operations that you can perform with those values
• Java is strongly Typed Language• Every Type in Java is one of the following: (i) Primitive Types( int, short, byte, long, char, float, double, boolean) (ii) A class Type (iii) An interface Type (iv) An array Type (v) null type [void is not type in java]
BITS Pilani, Pilani Campus
Java’s Primitive Types
Type Size Minimum Value Maximum Valuebyte 1 -128 +127
short 2 -32768 +32767
int 4 -2,147,483,648 +2,147,483,647
long 8 -9,223,372,036,854,775,808
+9,223,372,036,854,775,807
float 4 3.4e-038 3.4+038
double 8 1.7e-308 1.7e+308
char 2 0 65535
boolean 1 bit
BITS Pilani, Pilani Campus
C vs Java
• Does not have statements like goto, sizeof, typedef• Does not support data types as struct,union • No Explicit Pointer Type• No auto, extern, register, signed, unsigned • Java requires that function with no return type should be
explicitly declared as void void show() ; void print();• Java adds a new operator instanceof and >>> (unsigned
right shift)• Java adds a labelled break and continue statements• Return type of all conditional, logical or relational
expressions is boolean in java and not integer as in C.
BITS Pilani, Pilani Campus
Exampleclass test1{public static void main(String args[]){byte b = 24; byte b1 = 678;char x = 45;char y = 70000;char y1 = -25;short x1 = 238999;float f = 678.45;double f1 = 56.67;}}
D:\java\bin>javac test1.javatest1.java:6: possible loss of precisionfound : intrequired: bytebyte b1 = 678; ^test1.java:8: possible loss of precisionfound : intrequired: charchar y = 70000; ^test1.java:9: possible loss of precisionfound : intrequired: charchar y1 = -25; ^test1.java:10: possible loss of precisionfound : intrequired: shortshort x1 = 238999; ^test1.java:11: possible loss of precisionfound : doublerequired: floatfloat f = 678.45; ^5 errors
BITS Pilani, Pilani Campus
Example 2
int a=10;if(10)printf("Hello");elseprintf("Hi");}
OUTPUT
Hello
int a=10;if(10)S.O.P("Hello");elseS.O.P("Hi");}OUTPUTD:\java\bin>javac test100.javatest100.java:6: incompatible typesfound : intrequired: booleanif(a) ^1 error
In C In Java
BITS Pilani, Pilani Campus
Example 3% Operator
class test101{public static void main(String args[]){int a=100, b=90;System.out.println(a%b);double a1= 10.56, b1 =4.67;System.out.println(a1%b1);}}
D:\java\bin>java test101101.2200000000000006
BITS Pilani, Pilani Campus
Example 4 >>,<<,>>>
class test103{public static void main(String args[]){int x = -1024;System.out.println(x>>2);System.out.println(x<<2);System.out.println(x>>>2);}} D:\java\bin>java test103
-256-40961073741568
BITS Pilani, Pilani Campus
System.out.println()
• Prints/Displays output and shifts the print control to new line (Similar printf(“\n”) in C)
• Displays output only in String form• If parameter to it is not in String form then it
will be converted to string form by internally calling toString()
• + operator can be used to concatenate data from different types
BITS Pilani, Pilani Campus
Examples
• System.out.println(“Hello”+10);• System.out.println(10+20);• System.out.println(“10”+20);• System.out.println(“Hello: ”+20+”is my age”);
Note :+ opeartor is used for dual purpose addition,concatenation
Hello1030
1020
Hello20is my age
BITS Pilani, Pilani Campus
System.out.print()
• Prints/Displays output starting from the same line (Similar printf() in C)
• Displays output only in String form• If parameter to it is not in String form then it
will be converted to string form by internally calling toString()
• + operator can be used to concatenate data from different types
BITS Pilani, Pilani Campus
Examples
class test104{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");}}
D:\java\bin>java test104HelloI am fine It is OK
BITS Pilani, Pilani Campus
Example 2
class test105{public static void main(String args[]){System.out.print("Hello");System.out.print("I am fine");System.out.println(" It is OK");System.out.println(" It is OK Again");}}
D:\java\bin>java test105HelloI am fine It is OK It is OK Again