4.object Oriented Programming II
4.object Oriented Programming II
Module 4
Agenda
1 Method Overriding
2 Runtime Polymorphism
3 Abstract classes
4 instanceof Operator
5 Garbage Collection
Objectives
class A1 {
}
class A2 extends A1 {
}
class A3 {
public static void main(String[] args) {
A1 x;
A2 z = new A2();
x = new A2();//valid
z = new A1();//invalid
}
A Superclass Reference Variable Can Reference a Subclass Object
class Figure {
double dimension1;
double dimension2;
double area() {
System.out.println("Area of Figure is
undefined");
return 0;
}
}
Overridden Methods & Runtime Polymorphism - An Example (Contd.).
class FindArea {
public static void main(String args[]){
Figure f = new Figure(10,10);
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
Figure fig; //reference variable
fig = r;
System.out.println("Area of rectangle is :" +
fig.area());
fig = t;
System.out.println("Area of triangle is :" +
fig.area());
fig = f;
System.out.println(fig.area());
}
}
Runtime Polymorphism – Another Example
class BigB {
public void role() {
System.out.println(" My name is BigB");
}
}
BigB v;
// Parent class reference variable can point to
// any of its CHILD class objects....
• Often, you would want to define a superclass that declares the structure
of a given abstraction without providing the implementation of every
method
• The objective is to:
– Create a superclass that only defines a generalized form that will be
shared by all of its subclasses
– leaving it to each subclass to provide for its own specific
implementations
– Such a class determines the nature of the methods that the
subclasses must implement
– Such a superclass is unable to create a meaningful implementation
for a method or methods
Abstract Classes (Contd.).
• The class Figure in the previous example is such a superclass.
– You have only kinds of figures like Rectangle, Triangle etc. which
actually are subclasses of class Figure
• The subclasses cannot make use of the abstract method that they
inherit directly(without overriding these methods).
• These methods are sometimes referred to as subclasses’
responsibility as they have no implementation specified in the
superclass
Abstract Classes (Contd.).
• Any class that contains one or more abstract methods must also be
declared abstract
– It is perfectly acceptable for an abstract class to implement a
concrete method
– You cannot create objects of an abstract class
– That is, an abstract class cannot be instantiated with the new
keyword
– Any subclass of an abstract class must either implement all of the
abstract methods in the superclass, or be itself declared
abstract.
Revised Figure Class – using abstract
• There is no meaningful concept of area( ) for an undefined two-
dimensional geometrical abstraction such as a Figure
• This implies that class Figure be declared abstract, and all subclasses
derived from class Figure must override area( ).
Improved Version of the Figure Class Hierarchy
double area(){
System.out.print("Area of rectangle is :");
return dimension1 * dimension2;
}
}
class FindArea{
public static void main(String args[]){
Figure fig;
Rectangle r = new Rectangle(9,5);
Triangle t = new Triangle(10,8);
fig = r;
System.out.println("Area of rectangle is :" +
fig.area());
fig = t;
System.out.println("Area of triangle is :" +
fig.area());
}
}
The Role of the Keyword final in Inheritance
• The final keyword has two important uses in the context of a class
hierarchy. These uses are highlighted as follows:
• Using final to Prevent Overriding
– While method overriding is one of the most powerful feature of object
oriented design, there may be times when you will want to prevent
certain critical methods in a superclass from being overridden by its
subclasses.
– Rather, you would want the subclasses to use the methods as they
are defined in the superclass.
– This can be achieved by declaring such critical methods as final.
The Role of the Keyword final in Inheritance (Contd.).
It takes an object and a type and returns true if object belongs to that
type. It returns false otherwise.
Contd..
instanceof operator example 1
class InstanceOfImpl 1{
public static void main(String args[ ]) {
A a = new A( );
B b = new B( ); ob now refers to B
C c = new C( ); ob is also instance of A
A ob=b; Ob is not instance of C
if (ob instanceof B)
System.out.println("ob now refers to B");
else
System.out.println("Ob is not instance of B");
if (ob instanceof A)
System.out.println("ob is also instance of A");
else
System.out.println("Ob is not instance of A");
if (ob instanceof C)
System.out.println("ob now refers to C");
else
System.out.println("Ob is not instance of C");
}
}
instanceof operator example 2
class InstanceOfImpl 1{
public static void main(String args[ ]) {
A a = new A( );
B b = new B( ); Ob is not instance of B
C c = new C( ); ob is also instance of A
A ob=c; ob now refers to C
if (ob instanceof B)
System.out.println("ob now refers to B");
else
System.out.println("Ob is not instance of B");
if (ob instanceof A)
System.out.println("ob is also instance of A");
else
System.out.println("Ob is not instance of A");
if (ob instanceof C)
System.out.println("ob now refers to C");
else
System.out.println("Ob is not instance of C");
}
}
The Cosmic Class – The Object Class
1. abstract class A1 {
2. abstract final void m1();
3. }