Opp Overview02
Opp Overview02
OOP In Java
Lecture #01
Prof- Maqsood Ahemd Razi
Different Programming Paradigms
• Functional/procedural programming:
– program is a list of instructions to the computer
• Object-oriented programming
– program is composed of a collection objects
that communicate with each other
Main Concepts
• Object
• Class
• Inheritance
• Encapsulation
Objects
• identity – unique identification of an object
• attributes – data/state
• services – methods/operations
– supported by the object
– within objects responsibility to provide these
services to other clients
Class
• “type”
• object is an instance of class
• class groups similar objects
– same (structure of) attributes
– same services
• object holds values of its class’s attributes
Inheritance
• Class hierarchy
• Generalization and Specialization
– subclass inherits attributes and services from its
superclass
– subclass may add new attributes and services
– subclass may reuse the code in the superclass
– subclasses provide specialized behaviors (overriding
and dynamic binding)
– partially define and implement common behaviors
(abstract)
Encapsulation
• Separation between internal state of the object
and its external aspects
• How ?
– control access to members of the class
– interface “type”
What does it buy us ?
• Modularity
– source code for an object can be written and maintained
independently of the source code for other objects
– easier maintainance and reuse
• Information hiding
– other objects can ignore implementation details
– security (object has control over its internal state)
• but
– shared data need special design patterns (e.g., DB)
– performance overhead
Why Java ?
• Portable
• Easy to learn
myprog.c myprog.exe
gcc machine code
C source code
OS/Hardware
Platform Independent
myprog.java myprog.class
javac bytecode
Java source code
JVM
OS/Hardware
Primitive types
• int 4 bytes
• short 2 bytes
• long 8 bytes
Behaviors is
exactly as in
• byte 1 byte
C++
• float 4 bytes
• double 8 bytes
• char Unicode encoding (2 bytes) Note:
Primitive type
• boolean {true,false} always begin
with lower-case
Primitive types - cont.
• Constants
37 integer
37.2 float
42F float
0754 integer (octal)
0xfe integer (hexadecimal)
Wrappers
Is:
• In Java
Animal[][] arr=
new Animal[2][2]
// error :
public static Color getColor()
{ return myColor_; }
}
Static - [2/4] cont.
Usage:
System.out.println(“We have “ +
TeaPot.howManyTeaPots()+ “Tea Pots”);
Static - [3/4]
• Block
– Code that is executed in the first reference to the class.
– Several static blocks can exist in the same class
( Execution order is by the appearance order in the class
definition ).
– Only static members can be accessed.
class RandomGenerator {
private static int seed_;
static {
int t = System.getTime() % 100;
seed_ = System.getTime();
while(t-- > 0)
seed_ = getNextNumber(seed_);
}
}
}
String is an Object
• Constant strings as in C, does not exist