As we know that in order to declare a variable that references an object, we use the following
syntax.
ClassName variableName;
Here, variableName is the name of the reference variable and ClassName is the name of its class. Thus, variablename can reference any object of class ClassName. However, it can also reference any object whose class is a subclass of ClassName. For example: If a class A is a superclass of class B and class B is a superclass of class C then in that case, variable of class A can reference any object derived from that class (i.e. object of class B and class c). This is possible because each subclass object is an object of its superclass but not vice versa.
To illustrate how a superclass reference variable can refer the derive class object, let us consider the following example in which the class Dervl and Derv2 are derived from common superclass Base. All these classes have methods with the same name and we are accessing these methods using superclass reference variable.
class Base
{
public void display()
{
System.out.println("Base class display method is called");
}
}
class Derv1 extends Base
{
public void display()
{
System.out.println("Derv1 class display method is called");
}
}
class derv2 extends Base
{
public void display()
{
System.out.println("Derv2 class display method is called");
}
}
class polymorhism
{
public static void main(String[] args)
{
Base ptr; //Bae class reference variable
Derv1 dl = new Derv1();
Derv1 d2 = new Derv1();
ptr = dl; // ptr contain reference of Derv1 object
ptr.display();
ptr = d2; // ptr contain reference of derv2 object
ptr.display();
}
}
The output of the program reveals that the method display () of the subclasses are invoked. If the reference variable of the superclass Base contains a reference to Derv1 object, the display () method for that object will be called. Similarly, if the reference variable ptr contains a reference to Derv2 object, the () method for that object will be called. This means that one method call,
ptr().display;
can call different methods depending on the momentary contents of ptr:. This is how polymorphism is achieved. Notice that which overridden method will called is always determined by the type of the object referenced by the variable, not the type of the object reference variable. This task of determining which implementation of method will be used is performed by the JVM dynamically at run time. This is known as dynamic binding.
The polymorphic behavior can only be achieved if the following requirements are fulfilled.
• The method call for a subclass object must be through a superclass reference variable.
• The method called .must be declared in the superclass and defined in the subclass.
• The method in the superclass and subclass must have the same name and parameter list with the same number of parameters where corresponding parameters must be of the same type.
• The method return type must either be the same in the superclass and subclasses or must be covariant. Return types are said to be covariant if the return type of the method in the derived class is a subclass of the return type of the base class.
• The method access specifier must be no more restrictive in the subclass than in the superclass.
Visibility increases and Restriction decreases
private, none (if modifier used), protected, public
Now let us consider a practical example to understand the importance of polymorphism.
Consider Rectangle and Circle be the subclasses of class Shape. Suppose we want to calculate area of any shape so we will define an area () method in the Shape class and override it in the Rectangle and Circle subclass to calculate area of rectangle and circle.
class Shape
{
public void area()
{
System.out.println("Base class area method is called");
}
}
class Rectangle extends Shape
{
private double length,breadth;
Rectangle(double x,double y)
{
length = x ;
breadth = y ;
}
public void area ()
{
System.out.println("Area of Rectangle is = " + (length * breadth));
}
}
class Circle extends Shape
{
private double radius;
Circle(double r)
{
radius = r ;
}
public void area()
{
System.out.println("Area of Circle is = " + (Math.PI*radius));
}
}
class ShapeEx
{
public static void main(String[] args)
{
Shape s; //Shape class reference variable
Rectangle r = new Rectangle(10,20);
s = r; //Assingn rectangle reference to shape reference
s.area();
Circle c = new Circle(5);
s = c; //Assign circle reference to shape reference
s.area();
}
}