Module 4 - Polymorphism
Module 4 - Polymorphism
Thilagavathi M, AP(Sr.),SCORE
Types of Polymorphism
• Compile-time Polymorphism
• Runtime Polymorphism
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
1. It is the ability to create a variable, a function or an object that
has more than one form.
2. Polymorphism works with derived class objects. A single
variable of base class type can be created and made to
reference objects of any derived class, and to automatically call
the method that is specific to the type of the object, the
variable references.
3. To get polymorphic operation when calling a method, the
method must be a member of the base class as well as any
derived classes involved. A base class variable cannot call a
method of derived class, if it is not a member of base class.
4. Any definition of the method in a derived class must have the
same signature (name & arguments) and same return type as
in the base class, and must have an access specifier that is no
more restrictive. This is called method overriding.
Method overriding supports run time polymorphism
If the two methods are not identical and if only their names
are similar, then the two methods are simply overloaded.
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
class Shape //base class
{
int area(int l)
{
System.out.println("area will be calculated in Circle class");
}
}
Class Circle extends Shape
{
int area(int r) overridden
{
...
}
}
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
class Shape //base class
{
int area(int l)
{
System.out.println("area will be calculated in Circle class");
}
}
Class Circle extends Shape
{
int area(float r) overloaded
{
...
}
}
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
5. The method access specifier must be no more restrictive in the
derived class than in the base class.
if the method is defined as default in the base class, in the
derived class also, it must be defined with default
access mode or with public access mode. But it cannot be
defined with private access mode.
If in the base class, the method is defined as public, then
in the subclass, it can have only public access modifier. It
cannot even have default access modifier.
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
6. Deciding which overridden method to call depends on the type
of the object being stored and not on the type of the reference
variable.
Because a reference variable of super class can refer to an
object of any derived type, the kind of object that will be
referred will not be known, until the program executes.
Thus the choice of which method to execute has to be
made dynamically (run time) when the program is running
– it cannot be determined when the program is compiled.
This is called Dynamic method dispatch or late binding.
Dynamic method dispatch:
Mechanism by which a call to an overridden method is
resolved at run time, rather than at compile time. This is
how Java implements run time polymorphism.
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
class Shape //base class
{
int area(int l)
{
System.out.println("area will be calculated in Circle class");
}
}
Class Circle extends Shape
{
int area(int r) overridden
{
...
}
}
class shapemain
{
public static void main(String[] args)
{
Shape s;
s = new Shape( );
s.area(5); invokes Shape class area()
s = new Circle( );
s.area(5); invokes Circle class area()
}
}
Thilagavathi M, AP(Sr.),SCORE
Polymorphism
7. Deciding which method to call in the case of method overloading (functions with
same, but, they differ in terms of the number of arguments or type of arguments) is
done at compile time. For aspects that are performed at compile time, the type of
reference variable matters. For aspects which are performed at run time, the type of
the object being referred to, matters.
Thilagavathi M, AP(Sr.),SCORE