3-Java-OOP

146
8/8/2019 3-Java-OOP http://slidepdf.com/reader/full/3-java-oop 1/146 By Waqas 1 Object Oriented Programming

Transcript of 3-Java-OOP

Page 1: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 1/146

By Waqas 1

Object OrientedProgramming

Page 2: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 2/146

By Waqas 2

Object Oriented

Programming

Object

Class

Variables

instance variablesclass variables

Methods

instance methods

class methods

Constructors

Encapsulation

Inheritance

Composition

Polymorphism

Abstraction

Interfaces

 Nested Classes

Access Modifiers

  public, private,

 protected

Packages

Page 3: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 3/146

By Waqas 3

In object-oriented programming, a computer program may be seen as a

collection of individual units, or objects, that act on each other.

It is opposite to a traditional programming in which a program may be seen

as a collection of functions, or simply as a list of instructions to the

computer.

Each object is capable of receiving messages, processing data, and sending

messages to other objects.

Each object can be viewed as an independent little machine or actor with a

distinct role or responsibility.

Page 4: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 4/146

By Waqas 4

Object

Page 5: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 5/146

By Waqas 5

Object-oriented programming focuses on the

development of reusable software components, calledobjects.

An object is a building block 

which contains variables and methods.

Objects are key to understanding object oriented

technology.

You can look around and can see many examples of 

real-world objects: dog, car, table, chair, bicycle.

Page 6: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 6/146

By Waqas 6

All real world objects have two characteristics:

state and behavior

For example car have states

(current gear, number of gears, color, number of wheels)

and behaviors

(braking, accelerating, slowing down, changing gears)

Page 7: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 7/146

By Waqas 7

Software objects are modeled after real-world objects and they

also have state and behavior.

A software object maintains its state in one or more variable.

A software object implements its behavior with methods.

variables

(state)

Software Object

methods

(behavior)

Page 8: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 8/146

By Waqas 8

So our car object look like the following figure.

10 mph(speed)

5th gear

(currentgear)

red

(color)

Change

gear

brake

accelerate

Car Object

Page 9: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 9/146

By Waqas 9

Class

Page 10: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 10/146

By Waqas 10

In the real world, we often have many objects of the same

kind. For example, my car is just one of many cars in the

world.

Using object-oriented terminology, we can say that my car 

object is an instance of the class of objects known as cars.

Cars have state (4 gears, 1 engine, 4 wheels) and behavior 

(change gears, accelerate) in common. However, each car¶s

state is independent and can be different from each other.

Page 11: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 11/146

By Waqas 11

When car manufacturers build cars they take

advantage of the fact that cars share common

characteristics, by building many cars from the same

 blueprint.

It would be very inefficient to produce a new blueprint for every individual car manufactured.

In object-oriented, it is also possible to have many

objects of the same kind that share characteristics.Classes provide the benefits of creating a template of 

objects.

Page 12: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 12/146

By Waqas 12

we can take advantage of the fact that objects

of the same kind are similar and we can createa blueprint for those objects.

A template or blueprint of objects is called a

class.

³A class is a template or blueprint that defines the

variables and the methods common to all objects of 

a certain kind.´

Page 13: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 13/146

By Waqas 13

Change

gear

Brake

Number

of gears

Number of wheels

Car object

After you've created the car class, you can create

any number of car objects from the class. Eachobject gets its own copy of all the variables defined

in the class.

Page 14: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 14/146

By Waqas 14

Brake Brake

Change

gear Change

gear

speed = 15 speed =

10

Color =

red

Color =

blue

gears = 4 gears = 4

Your Car

These two car objects created from the car class.

My Car

Page 15: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 15/146

By Waqas 15

Creating

Classes

Page 16: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 16/146

By Waqas 16

class ClassName

{

variable 1;

variable 1; method1(){}

 method2(){}

}

Page 17: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 17/146

By Waqas 17

class Car

{

int gears;

int wheels;

 public void changeGear()

{}

}

Page 18: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 18/146

By Waqas 18

Creating

Objects

Page 19: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 19/146

By Waqas 19

You can create an object of class with the

following syntax: -

ClassName objVariable = new ClassName( );

So the car class object can be created as:

Car c = new Car( );

Page 20: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 20/146

By Waqas 20

The object creation statement has three parts.

Car c = new Car( );

Declaration Instantiation Initialization

1. Declaration

2. Instantiation

3. Initialization

Page 21: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 21/146

By Waqas 21

Declaration

You declare variables of int type as: -int a;

You can say that a is a variable who can refer 

to any type of int data.

Classes in java are also types so you can

declare class type variable as: -

Car c;You can say that c is a variable who can refer 

to any type of Car .

Page 22: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 22/146

By Waqas 22

Declaring a variable do not create any object.

The code Car c; does not create a new car object, it just declare a variable named c that

will be used to refer to a Car object. The

reference is still empty until assigned with a

new keyword.The empty reference is called null reference

in java.

c

Page 23: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 23/146

By Waqas 23

Instantiation

The new operator instantiates a class byallocating a memory for a new object. The

new operator returns a reference to the object

it created and this reference is assigned to the

appropriate variable.

Car object

c

Page 24: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 24/146

By Waqas 24

Initialization

The object is always initialize by calling aconstructor.

A constructor is a special method which has a

same name as class and used to initialize the

variables of that object.

In this case the Car class object is initialized by calling the constructor of Car class Car( );

Page 25: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 25/146

By Waqas 25

Constructor

Page 26: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 26/146

By Waqas 26

Constructor

Constructor is a special method in java class.

It is used to initialize a new object variables.

A constructor has the same name as the class.

For example if you have Color class the

constructor of color class is also Color( );

Page 27: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 27/146

By Waqas 27

Constructors cannot return any value not even void.

If we are not providing any constructor for a class thandefault (no parameter) constructor is automatically

 provided by the runtime system.

When we will create our own parameterize constructor.

We have to create our own default constructor as well

 because then default constructor is not available.

The constructor can be private, public or protected.

Page 28: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 28/146

By Waqas 28

Java supports name overloading of 

constructors so that a class can havenumber of constructors with the same

name.

The compiler will determine at run time

which constructor to call by matching the

number and type of arguments you are

 passing into the constructor.

Page 29: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 29/146

By Waqas 29

this

Java this keyword is used to invoke anymethod or variable of current class.

The syntax of method is: -

this.methodname( );

The syntax of variable is: -

this.variablename = 7;

Page 30: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 30/146

By Waqas 30

If you want to invoke another constructor of 

current class inside constructor you can use

this statement as follows:

this(4, 5);

The above statement will invoke the currentclass constructor that takes two int arguments.

Call to current class constructor with this

would be the first statement in theconstructor.

Page 31: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 31/146

By Waqas 31

Destroying

Objects(Garbage Collection)

Page 32: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 32/146

By Waqas 32

Some programming languages required thatyou keep track of all the objects you create and

then you explicitly destroy them when they are

no longer needed.

The Java platform allows you to create as

many objects as you want and you don't haveto worry about destroying them.

Object Destruction & Garbage Collection

Page 33: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 33/146

By Waqas 33

The Java runtime environment deletes objects when

it determines that they are no longer being used.

This process is called garbage collection.

An object is eligible for garbage collection whenthere are no more references to that object for 

example if object goes out of scope.

You can explicitly drop an object reference bysetting the variable to the special value null.

Page 34: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 34/146

By Waqas 34

If you want to invoke garbage collector in the

 program you can do this by issuing followingcommands.

System.gc();

Runtime.getRuntime().gc();

Page 35: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 35/146

By Waqas 35

finalize() method

If you want an object to clean up its state

 before it is deleted from the memory, you can

declare finalize() method in the class.

This method will be called by the garbage

collector before deleting any object of this

class.

Page 36: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 36/146

By Waqas 36

This method is inherited from the parent

Object class in all java classes and can beoverridden like the following code:

 protected void finalize(){

super.finalize();

// clean up code here.}

Page 37: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 37/146

By Waqas 37

Class

Variables(static variables)

Page 38: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 38/146

By Waqas 38

The runtime system allocates class variables once per class

regardless of the number of instances created of that class.

The system allocates memory for class variables the first

time it encounters the class at class load time.

All instances share the same copy of the class variables.

You can access class variables through an instance or through the class itself.

Class Variables

Page 39: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 39/146

By Waqas 39

To declare a class variable follow the

following syntax.

static Ty pe variable;

e.g.

static int a;

static double d;

If any object change the value of classvariable, the change will effect the values of 

all the variables.

Page 40: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 40/146

By Waqas 40

Because the class variable belongs to the

class rather than any specific variable, it can

 be accessed using the class name such as: -

ClassName.variableName;

Suppose you have class called Car and class

variable called gears than you can access that

variable like this: -

Car.gears;

Page 41: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 41/146

By Waqas 41

Class Variablevs.

Instance Variable

Page 42: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 42/146

By Waqas 42

All objects have their own

copy of instance variable.

All objects share the single

copy of static variable.

Instance variable is declared

without static keyword.

int a = 4;

Class variable is declared by

using static keyword.

static int a = 4;

Instance variableClass variable

Class vs. Instance Variables

Page 43: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 43/146

By Waqas 43

Instance variable cannot be

accessed without creating an

object.

ObjectName.variable

Static variable can be

accessed without creating an

object, by using class name.

ClassName.variable

Instance variable depends on

the object for which it isavailable.

Static variable does not

depend on the single object because it belongs to a class.

Page 44: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 44/146

By Waqas 44

Methods

Page 45: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 45/146

By Waqas 45

Method

A method is a group of programminglanguage statements with a given name.

Method syntaxReturnty pe methodname (parameters)

{

statement 1;

statement 2;«..

}

Page 46: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 46/146

By Waqas 46

void sum ( int a, int b )

{

statement 1

statement 2««..

}

Declaration

Body

Method has two major parts:

1. Method Declaration

2. Method Body

Page 47: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 47/146

By Waqas 47

Method that does not return a value

If a method does not return a value, itsreturn type must be declared void.

For example:

void abc( )

{System.out.println(³Hello World´);

}

Page 48: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 48/146

By Waqas 48

Method that returns a value

Methods that have a return type other than void return a value to the calling

routine using the following form of the

return statement.

return value;

int abc( ){

return 5+5;

}

Page 49: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 49/146

By Waqas 49

Class

Methods(static methods)

Page 50: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 50/146

By Waqas 50

The class (static) methods is not

referenced through a particular instance

of a class but through the class itself.

You don¶t have to create an object of the

class to invoke a static method.

Creating StaticMethods

Page 51: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 51/146

By Waqas 51

static Returnty pe methodname (parameters)

{

statement 1;

statement 2;

«..

}

StaticMethod Syntax

static void display(int a, int b)

{System.out.println(a+b);

}

example

Page 52: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 52/146

By Waqas 52

Class Methodsvs.

Instance Methods

Page 53: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 53/146

By Waqas 53

All objects have their own

copy of instance method.

All objects share the single

copy of static method.

instance methods are

declared without statickeyword.

Static methods are declared

 by using static keyword.

InstanceMethodStaticMethod

Static vs. InstanceMethods

Page 54: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 54/146

By Waqas 54

Instance method cannot be

invoked without creating anobject.

ObjectName.method( );

Static method can be

invoked without creating anobject, by using class name.

ClassName.method( );

Instance method depends on

the object for which it is

available.

Static method does not

depend on the single object

 because it belongs to a class.

Page 55: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 55/146

By Waqas 55

Instance methods can refer 

to this and super.

Static methods cannot refer 

to this or super.

  Non-static methods can

access static variables.

Static methods cannot

access not-static variables.

 Non-static methods can call

static methods.

Static methods cannot call

non-static methods.

Page 56: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 56/146

By Waqas 56

There are also static blocks of code in java,which have the following syntax:

static

{statements;

}

These blocks are used to initialize the

variables and execute at class load time.

Static Block s

Page 57: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 57/146

By Waqas 57

static

import

Page 58: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 58/146

By Waqas 58

static import

J2SE 5.0 introduces a new feature calledstatic import that lets you import the static

variable of a class so that you can refer to

them without having to mention the classname.

import static java.lang.Math.PI;

import static java.lang.System.out;

Page 59: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 59/146

By Waqas 59

Methodswith variable length

parameters

Page 60: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 60/146

By Waqas 60

Method with variable length parameters

J2SE 5.0 introduces a new feature that letsyou define methods with a variable number of 

  parameters, so that you can make several

method calls with a variable number of arguments.

These methods are called variable-lengthargument methods.

Page 61: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 61/146

By Waqas 61

Syntax

The variable-length parameters list consists of a type followed by three dots and the variable

name.

 public void  print(int... values)

{

for(int i: values)

{

System.out.println(i);}

}

Page 62: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 62/146

By Waqas 62

Note

There must be only one variable length parameter list in the method.

Following method is invalid

 public void  print(int... a, int... b)

{

}

Page 63: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 63/146

By Waqas 63

Note

If there are individual parameters in additionto the list, the variable length parameters list

must appear last inside the parameters of the

method.

Following method is valid:

 public void  print(String s, int... b){

}

Page 64: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 64/146

By Waqas 64

Method

Overloading

Page 65: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 65/146

By Waqas 65

Method Overloading

In java, it is possible to define two or moremethods with the same name, within the same

class.

They share the same name as long as their 

 parameters are different.

This process is known as ³methodoverloading´.

Page 66: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 66/146

By Waqas 66

When java call an overloaded method, it

simply executes the version of the method

whose parameters match the arguments.

Two methods are overloaded if there:

1. No of parameters are different

2.T

ype of parameters is different3. Order of parameters is different

Page 67: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 67/146

By Waqas 67

1. No of parameters are different

 public void add(int i)

{

System.out.println(i+i);

}

 public void add(int i, int j)

{System.out.println(i+j);

}

 public void add(int i, int j, int k)

{

System.out.println(i+j+k);

}

Page 68: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 68/146

By Waqas 68

2. Type of parameters is different

 public void add(int i, int j)

{

System.out.println(i+j);

}

 public void add(double i, double j)

{System.out.println(i+j);

}

 public void add(double i, int j)

{

System.out.println(i+j);

}

Page 69: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 69/146

By Waqas 69

3. Order of parameters is different

 public void add(double i, int j)

{

System.out.println(i+j);

}

 public void add(int i, double j)

{

System.out.println(i+j);

}

Page 70: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 70/146

By Waqas 70

Encapsulation(Data Hiding)

Page 71: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 71/146

By Waqas 71

Encapsulation

Packaging an object's variables within the protectivecustody of its methods is called Encapsulation.

age

get«

set« Calling Object

Page 72: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 72/146

By Waqas 72

A powerful benefit of encapsulation is the hiding of 

implementation details from other objects.

The internal portion (variables) of an object has

more limited visibility than the external portion

(methods).

This will protect the internal portion against

unwanted external access.

Page 73: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 73/146

By Waqas 73

ImplementingEncapsulation

 private int age;

First step is to create the variables of the object private so that nobody

can access them from outside.

Second step is to provide public setters and getters methods for 

accessing the private variables.

 public void setAge(int age)

{

this.age = age;

}

 public int getAge()

{

return this.age;

}

Page 74: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 74/146

By Waqas 74

 public class Student

{

 private String age;

 public void setAge(int age)

{

if(age<=0)

{

System.out.println(³Invalid Age´);

}else

{

this.age = age;

}

}

 public int getAge()

{

return this.age;

}

}

Implementing Business Logic with Encapsulation

Page 75: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 75/146

By Waqas 75

Inheritance(Deriving Classes)

Page 76: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 76/146

By Waqas 76

Inheritance is one of the most importantconcept in object-oriented programming, and it

has a direct effect on how you design and write

Java classes.

Inheritance is a powerful mechanism that

allows a class to inherit functionality from an

existing class.

For example, sports car, luxury car, both are

kinds of cars.

Inheritance

Page 77: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 77/146

By Waqas 77

Car

Sports Car Luxury Car

S ports Car and Luxury Car are sub classes or child  classes of 

Car class and Car class is the super  or parent  class of Luxury

Car and S ports Car.

Page 78: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 78/146

By Waqas 78

Each subclass inherit s all variables and methods

from the super class except private variables and

methods.

However, subclasses are not limited to the variables

and methods provided to them by their super class.

Subclasses can add variables and methods or can

change the definition of the existing methods

according to their own requirements.

Page 79: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 79/146

By Waqas 79

We use extends keyword to create child classin java as follows:

class child-class extends parent-class

{

Class bod y

«

}

Deriving Classes

Page 80: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 80/146

By Waqas 80

class Shape

{

}

class Oval extends Shape

{

}

class Triangle extends Shape

{

}

Page 81: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 81/146

By Waqas 81

The object of child class can call any non

 private method from the super class that is why

we can say that:

A child class object is a parent class object.

As there is no multiple inheritance in java so In

 java every class can extend only one class.However one parent class may have more then

one child classes.

Page 82: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 82/146

By Waqas 82

The topmost class in the java class

hierarchy is called ³Object´. If you

declare a class which does not sub

class of any super class than your 

class is considered to be the child

class of the class Object.

Java Class Hierarchy 

Java Class Hierarchy

Page 83: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 83/146

By Waqas 83

Java Class Hierarchy 

C C i O

Page 84: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 84/146

By Waqas 84

When you create an object of child class thenchild class constructor first calls the parent

class default constructor before it performs its

own initialization.

The parent class constructor calls its super 

class constructor and so on.

This is necessary because if the parent class isnot properly initialize how the child class

object can be created properly.

Constructor Calling Order

Page 85: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 85/146

By Waqas 85

If you don¶t want to invoke the default constructor 

of super class but want other constructor than you

use super to invoke the parent class constructor.

super(4);

This will invoke the parent class constructor that

would take one int argument. Call to parent class

constructor with super would be the first statementin the child class constructor.

super

Page 86: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 86/146

By Waqas 86

You can also invoke any method or variable of 

super class in the child class with the super 

keyword.

super.methodname( );

super.variablename = 7;

Page 87: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 87/146

By Waqas 87

Method

Overriding

Page 88: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 88/146

By Waqas 88

Method Overriding

When a child class defines a method with

the same name and signature as the parent,

then it is said that the child¶s version

overrides the parent¶s version in his favor.

When an overridden method is called from

child class object, the version in the child

class is called not the parent class version.

Page 89: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 89/146

By Waqas 89

The return type, method name, number and

type of the parameters of overridden method

in the child class must match with themethod in the super class.

You can call the super class method fromthe child class with super keyword.

super.overriddenMethodName( );

Page 90: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 90/146

By Waqas 90

Final

Keyword

final keyword

Page 91: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 91/146

By Waqas 91

final keyword

The keyword final has three usages:

1. Final variables.

2. Final methods.

3. Final classes.

1. Final variables

Final modifier used with the variables specify that

the variable has a constant value and it can not

changed.

final int age = 10;

2 Fi l th d

Page 92: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 92/146

By Waqas 92

2. Final methods

When we use the keyword final with methodsthen it specifies that the method cannot be

overridden in the child class.

final void volume( )

{

}

3 Fi l l

Page 93: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 93/146

By Waqas 93

3. Final classes

The keyword final can be applied to classes.If this is done, the class cannot be inherited.

This provides security features and stops

further extensions of the class.

final class City

{

}

Page 94: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 94/146

By Waqas94

Composition

Composition

Page 95: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 95/146

By Waqas95

Composition

Composition is the ability of composing

other objects inside classes.

Car Object

Engine Object

doubleweight;

String

make;

String color 

int wheels

Engine e;

public class Car

Page 96: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 96/146

By Waqas 96

 public class Car

{

String color;

int wheels;

Engine e; // Single Object

Gear g[]; // Multiple Objects

}

 public class Engine{

double weight;

String make;

}

 public class Gear

{

««

}

Page 97: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 97/146

By Waqas 97

Polymorphism(Late Binding or Dynamic Binding)

Polymorphism

Page 98: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 98/146

By Waqas 98

Polymorphism is the ability to assume different

forms.

In object-oriented programming, this refers to the

ability of objects to have many methods with differentimplementations.

In Polymorphism program does not need to know theexact type of object and method in advance.

Polymorphism

Page 99: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 99/146

By Waqas 99

Caller 

A

B

C

Exchange decide at call time to which

department it may transfer the callaccording to the request of the caller.

Polymorphism is a phenomena in which the parent

Page 100: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 100/146

By Waqas 100

Polymorphism is a phenomena in which the parent

class object call the appropriate overridden method of 

child class at run time.

Shape

 public void 

draw(){

}Oval

 public void draw()

{

}

Rectangle

 public void draw()

{

}

Page 101: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 101/146

By Waqas 101

Abstraction

Abstraction

Page 102: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 102/146

By Waqas 102

Abstraction

Abstraction is a design technique that focuses on the essentialaspects of an entity and ignores or conceals less important or 

non-essential aspects.

A process of identifying which details in a given context areessential, and should be visible, and which are non-essential

and can be hidden "behind the scenes".

Abstraction is an important tool for simplifying a complex

situation

Abstract Classes

Page 103: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 103/146

By Waqas 103

Abstract Classes

An abstract  class is a class that is partiallyimplemented and whose purpose is solely

represent abstract concept.

Abstract classes are made up of one or more

abstract methods.

Abstract classes cannot be instantiated becausethey represent abstract concept.

Page 104: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 104/146

By Waqas 104

For example: Food represents the abstract concept

of things that we can eat, that is why we have never 

seen the instance of food what we see instead are

instances of burger, apple, chocolate.

In Java classes such as Number represents the

abstract concept of numbers therefore it not be

instantiated.

What we instantiate the subclasses of Number such

as Float, Integer etc.

Abstract Class Declaration

Page 105: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 105/146

By Waqas 105

 public abstract class Food 

{

}

Abstract Class Declaration

As Food is abstract class so the following statement will not

compile.

Food f = new Food();

AbstractMethods

Page 106: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 106/146

By Waqas 106

AbstractMethods

An abstract method is a method which has no

implementation (body).

The body of this method is provided by a subclass of 

the class in which the abstract method is declared.

The syntax of abstract method is:

 public abstract void draw( );

Abstract Classes &Methods Concepts

Page 107: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 107/146

By Waqas 107

Abstract Classes &Methods Concepts

Abstract class can not be instantiated it meansthat you can not create object of abstract class

with new keyword.

Abstract class can only be sub classed.

A class derived from abstract class must override

all of the parent class¶s abstract methods. If it

does not define all methods then it will declareitself as abstract.

Page 108: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 108/146

By Waqas 108

An abstract class can inter mix abstract and non

abstract methods.

Abstract class and method cannot be declared as

final.

abstract method cannot be declared as static.

Abstract method cannot be declared as private.

Page 109: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 109/146

By Waqas 109

Interface

Interface

Page 110: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 110/146

By Waqas 110

An interface defines a protocol of behavior that can be implemented by any class anywhere in the class

hierarchy either they are related or not.

An interface defines a set of methods but does notimplement them.

A class that implements the interface agrees to

implement all the methods defined in the interface.

Hierarchy A Hierarchy B

Page 111: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 111/146

By Waqas 111

Interface

Interface is a bridge between two related or unrelated classes

so that they can share common data and implementations and

communicate with each other.

Declaring Interface

Page 112: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 112/146

By Waqas 112

g

 public interface Database{

 public final static int a = 4;

 public final static int b = 5;

 public abstract void insert( ); public abstract void update( );

}

All variables are public final and static by default

All methods are public abstract by default

Implementing Interface

Page 113: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 113/146

By Waqas 113

T

he above class must need to define all the methods in theinterface Data and Driver, otherwise it declares itself 

abstract.

 public class Oracle implements Database

{

 public abstract void insert( )

{}

 public abstract void insert( ){}

}

ImplementingMultiple Interfaces

Page 114: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 114/146

By Waqas 114

 public class Oracle implements Database,

Connection, Command {

}

p e e g u p e e ces

Class can implement more than one interface.

 Now this class has to define all the abstract methods of Database, Connection and Command interfaces.

Interfaces Inheritance

Page 115: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 115/146

By Waqas 115

An interface can extend another interface.

 public interface Database

{

}

 public interface M ySq l extends Database

{

}

Interfaces andMultiple Inheritance

Page 116: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 116/146

By Waqas 116

An interface can extends any number of interfaces.

p

 public interface Database

{

} public interface Connection

{

}

 public interface M ySq l extends Database,

Connection

{

Page 117: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 117/146

By Waqas 117

Interface

vs.

Abstract Class

Because an interface is simply a list of unimplemented, and

Page 118: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 118/146

By Waqas 118

Because an interface is simply a list of unimplemented, and

therefore abstract, methods, you might wonder how an interface

differs from an abstract class.

An abstract class can define some

methods.

An interface cannot define any

method.

Abstract class is declared with theclass keyword.

Interface is declared with theinterface keyword.

Abstract ClassInterface

Page 119: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 119/146

By Waqas 119

Abstract classes are part of the

class hierarchy.

Interfaces are not part of the class

hierarchy.

We can not extend more then one

abstract class.

We can implement multiple

interfaces on a single java class.

We extends abstract classes using

extends keyword.

We implement interface on class

using implements keyword.

Abstract ClassInterface

Page 120: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 120/146

By Waqas 120

Nested

Classes

Nested Classes

Page 121: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 121/146

By Waqas 121

Nested Classes

Java lets you define a class as a member of 

another class. These classes are called nested

classes.

As a member of its enclosing class, a nestedclass has a special privilege:

It has unlimited access to its enclosing class's

members, even if they are declared private.

 public class Shape

Page 122: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 122/146

By Waqas 122

Like static variables and methods, a nested class can

also be declared static.

A static nested is called just a static nested class.

A non-static nested class is called inner class.

{

 public class Point

{

}

}

Creating Object of Nested Class

Page 123: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 123/146

By Waqas 123

g j

 public class Shape

{

Point p = new Point();

 public class Point

{

Shape s = new Shape();

}

}

Inner class

object

Outer class

object

Creating Object of Nested Class in Third Class

Page 124: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 124/146

By Waqas 124

 public class Shape

{ public class Point

{

}

}

Inner class

ob ject

 public class Test

{

 public static void  main(String args[])

{Shape.Point p = new Shape().new

Point();

}

}

Extending Nested Class through Inheritance

Page 125: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 125/146

By Waqas 125

 public class Shape{

 public class Point

{

}

} public class Oval extends Shape

{

 public class CenterPoint extendsShape.Point

{

}

}

Some Facts about Nested Classes

Page 126: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 126/146

By Waqas 126

1. Like static methods static nested class cannot directlyaccess instance methods and variables.

1. Nested classes can be declared abstract.

1. Nested classes can be declared final.

1. Nested classes can also be declared private, public,

 protected or default just as other member methods.

Page 127: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 127/146

By Waqas 127

Access

Modifiers

Access Modifiers

Page 128: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 128/146

By Waqas 128

Access modifiers define various levels of access between

class members and the outside world (other objects).

They allow us to define the encapsulation characteristics of 

an object.

There are four access modifiers in java:

1.  private

2 .  protected 

3 .  public

4. default.

private

Page 129: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 129/146

By Waqas 129

The private access modifier is the most

restrictive; it specifies that class members areaccessible only by the class in which they are

defined. This means that no other class has

access to private class members, evensubclasses.

 private int a = 4;

 private void show ( )

{

}

BusCar Burg

erPizza

Page 130: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 130/146

By Waqas 130

  private variable a is only accessible in Car 

class.

Bus

Package 1 Package 2

Carprivate int a;

BurgerPizza

protected

Page 131: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 131/146

By Waqas 131

This modifier specifies that class members

are accessible only to methods in that class,the classes inside the package and subclasses

of that class outside the package.

 protected int a = 4;

 protected void show ( )

{

}

BusCar Burg

erPizza

Page 132: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 132/146

By Waqas 132

  protected variable a is accessible inside Car 

class and inside child class which can be insame package or different package.

Bus

Package 1 Package 2

CarProtected int a;

BurgerPizza

public

Page 133: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 133/146

By Waqas 133

The public access modifier specifies that

class variables and methods are accessible toanyone, both inside and outside the class.

This means that public class members have

global visibility and can be accessed by anyother object.

 public int a = 4;

 public void show ( )

{

}

BusCar Burg

erPizza

Page 134: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 134/146

By Waqas 134

  public variable a is accessible from all 4

classes in any package.

Bus

Package 1 Package 2

Carpublic int a;

BurgerPizza

DefaultAccess Modifier

Page 135: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 135/146

By Waqas 135

The default (package) access modifier 

specifies that only classes in the same packagecan have access to a class's variables and

methods.

There is no actual keyword for declaring the

default access modifier.

it is applied by default in the absence of anyaccess modifier.

BusCar Burg

erPizza

Page 136: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 136/146

By Waqas 136

Int variable a is accessible only inside Package

1.

Package 1 Package 2

int a;

g

Page 137: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 137/146

By Waqas 137

Packages

Packages

Page 138: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 138/146

By Waqas 138

A package is a collection of related classes and

interfaces.

Packages provide following benefits:

Classes are easier to find and use No naming conflictions

Control access for class members

For example you have classes graphics objects likeCircle, Square, Rectangle etc.

You should bundle these classes into package for the

Page 139: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 139/146

By Waqas 139

You should bundle these classes into package for the

following reasons:

1. You are other programmers can easily determine

that these classes are related.

1. You and other programmers know that where to

find classes that provide graphics related functions.

1. The name of your class wouldn¶t conflict with class

names in other packages.

Creating Packages

Page 140: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 140/146

By Waqas 140

To create a package we put the package

statement at the top of the source file in

which the classes and interfaces are defined.

 package graphics;

 public class Circle

{

}

To put Rectangle the class inside graphics package,

the following code appears inside the Rectangle java

Page 141: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 141/146

By Waqas 141

the following code appears inside the Rectangle.java

file.

 Now both Circle and Rectangle classes are in the

graphics package.

 package graphics;

 public class Rectangle

{

}

if you want Circle class to available outside

Page 142: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 142/146

By Waqas 142

if you want Circle class to available outside

the package. You must use the class fullyqualified name as follows:

graphics.Circle c = new graphics.Circle( );

This makes your code difficult to read.

import Statement

Page 143: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 143/146

By Waqas 143

Java provide a solution to limit the use of fully qualified name

each time you create an object.

You can just import the class you need with import statement.

To import a specific member into the current file, put an import

statement at the beginning of your file before any class or 

interface definitions but after the package statement, if there is

one.

Here's how you would import the Circle class from thegraphics package

import graphics.Circle;

Page 144: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 144/146

By Waqas 144

 Now you can refer to Circle class by its simple name:

Circle c = new Circle( ):

To import all the classes in the graphics package you

can use * as wild card character as follows:

import graphics.*;

Page 145: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 145/146

By Waqas 145

WaqasAnwar

Sun Certified Java Programmer (SCJP)

Sun CertifiedWeb Component Developer for J2EE (SCWCD)

Microsoft Certified Professional (VB.NET)Microsoft Certified Professional (C# .NET)

Microsoft Certified Professional (ASP.NET)

Microsoft Certified Professional (XMLWeb Services .NET)

Microsoft Certified ApplicationDeveloper (MCAD .NET)

http://www.ezzylearning.com

Page 146: 3-Java-OOP

8/8/2019 3-Java-OOP

http://slidepdf.com/reader/full/3-java-oop 146/146