in Core Java

Polymorphism in Java

Polymorphism in Object oriented Programming

The definition of Polymorphism as per the dictionary is 'The occurrence of something in different forms,in particular'

As per biology, the polymorphism is nothing but exhibiting different forms by a species. Poly means many. The same principle is applicable in Object oriented programming where the Objects at runtime decide what behavior will be invoked.

Polymorphism is implemented in Java using method overloading and method overriding concepts.

 

Polymorphism with example:

Polymorphism in Object oriented progrmaming

Let us Consider Car example for discussing the polymorphism. Take any brand like Ford, Honda, Toyota, BMW, Benz etc., Everything is  of type Car.

But each have their own advanced features and more advanced technology involved in their move behavior.

Now let us create a basic type Car

Car.java


public class Car {
  
    int price;
    String name;
    String color;
        
    public void move(){
    System.out.println("Basic Car move");
    }
      
}

Let us implement the Ford Car example.

Ford extends the type Car to inherit all its members(properties and methods).

Ford.java


public class Ford extends Car{
  public void move(){
    System.out.println("Moving with V engine");
  }
}

The above Ford class extends the Car class and also implements the move() method. Even though the move method is already available to Ford through the Inheritance, Ford still has implemented the method in its own way. This is called method overriding.

Honda.java

public class Honda extends Car{
  public void move(){
    System.out.println("Move with i-VTEC engine");
  }
}

Just like Ford, Honda also extends the Car type and implemented the move method in its own way.

Method overriding is an important feature to enable the Polymorphism. Using Method overriding, the Sub types can change the way the methods work that are available through the inheritance.

PolymorphismExample.java


public class PolymorphismExample {
  public static void main(String[] args) {
    Car car = new Car();
    Car f = new Ford();
    Car h = new Honda();
    
    car.move();
    f.move();
    h.move();
    
  }
}

Polymorphism Example Output:

Polymorphism example output

In the PolymorphismExample class main method, i have created three objects- Car, Ford and Honda. All the three objects are referred by the Car type. 

Please note an important point here that A super class type can refer to a Sub class type of object but the vice versa is not possible. The reason is that all the members of the super class are available to the subclass using inheritance and during the compile time, the compiler tries to evaluate if the reference type we are using has the method he is trying to access. 

So, for the references car,f and h in the PolymorphismExample, the move method exists from Car type. So, the compiler passes the compilation process without any issues.

But when it comes to the run time execution, the virtual machine invokes the methods on the objects which are sub types. So, the method move() is invoked from their respective implementations.

So, all the objects are of type Car, but during the run time, the execution depends on the Object on which the invocation happens. This is called polymorphism.

Polymorphism using abstract and interface:

Polymorphism can be implemented using the abstract and interface keywords, which enforce to implement the methods in the sub types. More on this on Method overriding and overloading.

As per the Object oriented analysis and Design, polymorphism is a very important feature to change the behavior of the application in the run time. By changing the object on which the method is invoked, we have the control to change the behavior of the application.

There are lot of design patterns that depend on this concept to make complex things simple.

Summary:

Polymorphism is a very important concept in object oriented programming which enables to change the behavior of the applications in the run time based on the object on which the invocation happens. This enables to easily change the system without making much changes to the application. Polymorphism is implemented using the concept of Method overloading and method overriding. This can only happen when the classes are under the parent and child relationship using inheritance.

Srinivas Reddy

Srinivas Reddy

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...

More Posts - Website

Follow Me:
TwitterFacebook

Related Posts

Srinivas Reddy
Written By:

An IT - Specialist with loads of experience in Java, J2ee platforms and loves to share his experience on technology with You...