INTRODUCTION TO JAVA PROGRAMMING
LANGUAGE
(ELECTIVE-1)
LECTURE-21
Today’s Agenda
• Polymorphism
• Early and Late Binding
• Abstract Classes and Methods
Polymorphism
Dynamic Method Dispatch
• The word polymorphism means the ability to behave
differently in different situations. Poly – Many , Morphs –
Forms.
• We have seen polymorphism in methods through methods
Overloading and Overriding.
• Now, we shall see how we can achieve polymorphic behavior
using a single base class reference and call different methods
of derived class. Which is called Dynamic method dispatch.
• Before we can understand polymorphism, we need to
understand a very term in programming i.e. Binding.
Binding
• The term binding means a mechanism followed by
the compiler of a language to make
function/method calls.
• Just like Object oriented languages Java also
follows two types of binding –
1. Early/Compile time/Static Binding
2. Late/Dynamic/Runtime Binding
Binding
• In Early Binding, if the method being called is a
static method then Java determines the version of
method by looking at the type of reference being
used and not the object.
• In Late Binding, if the method being called is non
static method then Java determines the version of
method to be called by looking at the object being
pointed and not the reference.
Example
class A class B extends A
{ {
public static void show() public static void show()
{ {
S.o.p("In show of A"); S.o.p("In show of B");
} }
public void display() public void display()
{ {
S.o.p("In display of A"); S.o.p("In display of B");
} }
} }
class Test
{
public static void main(String [ ] args)
{
A ref;
ref=new A( );
ref.show( );
ref.display( );
ref=new B( );
ref.show( );
ref.display( );
}
}
A Program to achieve
Dynamic Method Dispatch
class Shape public String name()
{ {
private int dim1; return “Unknown”;
private int dim2; }
public Shape(int dim1, int dim2) public int getDim1()
{ {
this.dim1=dim1; return dim1;
this.dim2=dim2; }
} public int getDim2()
public double area() {
{ return dim2;
return 0.0; }
} }
class Rectangle extends Shape class Triangle extends Shape
{ {
public Rectangle(int l, int b) public Triangle(int b, int h)
{ {
super(l,b); super(b, h);
} }
public double area() public double area()
{ {
return(getDim1()*getDim2()); return(0.5*getDim1()*getDim2());
} }
public String name() public String name()
{ {
return “Rectangle”; return “Triangle”;
} }
} }
class UseShape
{
public static void main(String [ ] args)
{
Shape s;
s=new Rectangle(5,10);
S.o.p(“Shape is ”+s.name());
S.o.p(“Its area is ”+s.area());
s=new Triangle(15,20);
S.o.p(“Shape is ”+s.name());
S.o.p(“Its area is ”+s.area());
}
}
Abstract Classes
and Methods
• Some methods of a class cannot be defined as their
functionalities depends on derived classes, like in the
previous example methods area() and name().
• So to avoid writing their body and just declare them in base
class so that they can be overridden in derived class as per
their functionality, we use the keyword abstract.
• By declaring a method abstract we can avoid defining the
method, but with two compulsions
1. The class should also be prefixed with the keyword
abstract.
2. If the class is made abstract then its object cannot be
created but reference can be created.
From Previous
Example
abstract class Shape
{
private int dim1;
private int dim2;
// ---same as previous---//
abstract public String name( );
abstract public double area( );
}
}
* Rest both derived classes and Driver class remains
same. Program will show same output.
Methods which cannot be
made Abstract
• static methods – Since, abstract is used when there is “no
functionality defined yet” and static itself means “there is
functionality even if you do not have object”. So, static and
abstract are completely opposite to each other.
• Constructors – Since, constructors are never inherited hence
don’t require to be overridden.
• private methods – These are not accessible in derived class.
• Classes which inherits abstract methods must compulsorily
override the abstract method or the derived class itself should be
made abstract and thus, derived class objects cannot be created.
End Of Lecture 21