by Dinesh Thakur Category: Introduction to Java

Polymorphism is a very powerful concept that allows you to design amazingly flexible applications. The word 'Polymorphism: is derived form two Greek words 'poly' which means many and 'morphos' which means forms. So, polymorphism means the ability to take many forms.

Polymorphism can be defined as a one interface muitiple methods which means that one interface can be used to perform different but related activities. In programming terms, it means the ability by which a single variable of given type can be used to reference objects of different types and to automatically call the method that is specific to the type of object the variable references. This enables a single method call to behave differently depending on the type of object to which the call applies.

In order to understand this concept, consider an example of collection of shapes in a Graphic package. Each shape in it may be completely different as eash may be of different structure and have different way of drawing such as rectangle, circle, triangle etc, If we want to draw different shapes, the same task has to be performed in principle (i.e, draw a shape) but depending on the shape, it may look very different as method for drawing each shape is different. The user's request in everyday language to draw a shape will therefore lead to various'different actions. In order to simulate this, a separate version of draw a shape needs to be implemented for every class that describes a kind of shape (derive class). The class hierarchy is as shown in the following figure.

class hierarchy

In the above figure, draw ()'method is present in the base class Shape to draw any shape. As the technique for drawing different shapes is unique so we have redefined (override) the draw () method in each of the subclass. With polymorphism, whenever a user wants to draw a particular shape (say circle), it simply sends the request to the super class Shape by invoking its method draw () which in reality be calling a method draw () of the appropriate sub class so as to display the requested shape.

The advantage of polymorphism is that even for such heterogenous collection of different shapes, the general draw () method can be invoked, which automatically results in respective method being called according to the actual type of shape. In other words, user need not to be concerned about which draw () method is executed in order to fulfil its requests. In the above example, the single interface is the draw () method present in the base class Shape and different tasks are the draw () method present in the subclasses.

So, polymorphism can also be defined as the ability of an object of different classes (sub classes) to respond to the same request each in its own way.

The main benefit of polymorphism is thaLit reduces the complexity by allowing the same name to perform different tasks. As in the above example, same named method draw () can be used for drawing any kind of shape.

Another benefit of polymorphism is that it is possible to design and implement systems that are easily extensible. New classes can be added in the class hierarchy with little or no modification in the super class. For example, if user wishes to draw another shape Ellipse then we need to add Ellipse class in the class hierarchy and there is no need to modify any code in the super class Shape.

Before implementing polymorphism, you need to understand how superclass reference variable can refer to a subclass object.

Referencing Subclass Object using Superclass Reference Variable

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 program in which the class Derv1 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.

//Show how superclass reference variable contain subclass object

//base class

class Base {

    public void display() {

        System.out.println("Base class display method is called");

    }

}

//derived class 1 that extends class Base

class Derv1 extends Base {

      public void display() {

        System.out.println("Derv1 class display method is called");

     }

}

//derived class 2 that extends class Base

class Derv2 extends Base {

    public void display() {

       System.out.println("Derv2 class display method is called");

   }

}

class PolymorphismDemo {

     public static void main (String[] args) {

        Base ptr; //Base class reference variable

       Derv1 d1 = new Derv1();

       Derv2 d2 = new Derv2();

       ptr = d1; // ptr contain reference of derv1 object

      ptr.display();

     ptr = d2; // ptr contain reference of derv2 object

     ptr.display() ;

}

}

Output :

Derv1 class display method is called

Derv2 class display method is called

Explanation: The output of the program reveals that the method display () of the subclasses are invoked. If the reference variable ptr 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, display() 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.

Example Output

The polymorphic behaviour 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.

//Show how superclass reference variable contain subclass object

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*radius)); }

}

class ShapeDemo {

    public static void main(String[] args) {

     Shape s; //Shape class reference variable

     Rectangle r = new Rectangle(10,20);

     s = r; //Assign rectangle reference to shape reference

     s.area ();

    Circle c = new Circle(5);

    s = c; //Assign circle reference to shape reference

    s.area ();

}}

Output Area of Rectangle is = 200.0

Area of Circle is = 78.53981633974483

 



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.