Polymorphism in Java

 

In java programming language polymorphism is a concept through which a single operation or action can be performed in many different ways. The word polymorphism is derived from two Greek words that are poly and morphs. By poly we mean many and by morphs we mean forms. Therefore, the name tells that polymorphism means many forms.

In java programming language there are two different types of polymorphism:

  1. Compile time polymorphism
  2. Runtime polymorphism

In java programming language polymorphism can be performed by method overloading and by method overriding.

An example of compile time polymorphism is when we overload a static method in our code.

 

Runtime polymorphism in Java:

In java programming language the runtime polymorphism can also be called as dynamic method dispatch. It is a process in which the calling of an overridden method or function is done at run time. This calling is not done at the compilation time of the program.

In runtime polymorphism the method that is overridden is invoked or called by using a reference variable. This reference variable is of the super or parent class. Which method or function will be called is decided by the object that is referred to by the reference variable of the super or parent class.

 

Upcasting:

Before understanding the concept of runtime polymorphism you should go through the concept of upcasting in java programming language.

Upcasting is a concept in which the reference variable of the super or parent class is used to refer to the object of the child or subclass. Consider the following example to understand this:

CODE:

class D {}

class E extends D {}

CODE:

D d1 = new E ();

In the above lines of code there are two classes D, the parent class and E, the child class. The reference variable of the super class is referring to the object of child class. This is called upcasting in Java.

 

Example of Java runtime polymorphism:

Consider the following example in which the concept of upcasting is implemented:

CODE:

class Vehicle {

void display () {

system. out. printlln (“Vehicle is running”);

}

class Car extends vehicle {

void display () {

system. out. println (“Car is running”);

}

public static void main (string args []) {

Vehicle v1 = new Car ();

v1. display ();

}

OUTPUT:

Car is running

In the above example, two classes are created Vehicle, the parent class, and Car, the child class. The method display () of the vehicle class is overridden by the child class. And this method is called or invoked by using the reference of the variable of the parent class.

The calling of the method is determined by JVM and not by compiler therefore, we can refer it as the run time polymorphism.

 

Real example of runtime polymorphism:

Consider that you have a class that is named as Animal and now you have three child classes that fall in the category of animal. For example you can take cat, dog, Lion as the child classes and Animal as parent class. Now in all of the child classes you have a display () method that will have the same implementation and that will have same parameters.

Note that in this example we will be using upcasting that we didn’t when in method overriding.

Consider the following diagram:

real-example-of-runtime-polymorphism

Now consider the following code in which we have implemented the above scenario:

CODE:

class Animal {

void display () {

system. out. println (“Animals that are mammal”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cats are mammal”);

}

}

class Dog extends Animal {

void display () {

system. out. println (“Dogs are mammal”);

}

}

class Lion extends Animal {

void display () {

system. out. println (“Lions are mammal”);

}

}

class ABC {

public static void main (string [] args) {

Animal a1 = new Cat ();

Animal a2 = new Dog ();

Animal a3 = new Lion ();

a1. display ();

a2. display ();

a3. display ();

}

}

OUTPUT:

Cats are mammal

Dogs are mammal

Lions are mammal

In the above example Animal is the parent class and cat, dog, and lion are the child classes. We used method overriding in the above program and the method display () is overridden.

We used the reference variable of the parent class to refer to the object of the child class and in this way all overridden methods are executed and the required output is got.

 

Java runtime polymorphism with data member:

In Java programming language the run time polymorphism cannot be achieved by data members as the data members cannot be overridden. The runtime polymorphism is achieved through overriding methods.

Consider the following example in which there is a parent class and a child class and both the classes have same data member:

CODE:

class Vehicle {

int velocity = 50;

}

class Car extends Vehicle {

int velocity = 100;

public static void main (string args []) {

Vehicle v1 = new car ();

system. out. println (v1. velocity);

}

OUTPUT:

50

In the above example there is no run time polymorphism as none of the methods is overridden.

 

Java runtime polymorphism with multilevel inheritance:

Consider the following example in which there is run time polymorphism with multilevel inheritance:

CODE:

class Animal {

void display () {

system. out. println (“Animals that are mammal”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cats are mammal”);

}

}

class Dog extends Cat {

void display () {

system. out. println (“Dogs are mammal”);

}

}

class Lion extends Dog {

void display () {

system. out. println (“Lions are mammal”);

}

}

class ABC {

public static void main (string [] args) {

Animal a1, a2, a3, a4;

a1 = new Animal ();

a2 = new Cat ();

a3 = new Dog ();

a4 = new Lion ();

a1. display ();

a2. display ();

a3. display ();

a4. display ();

}

}

OUTPUT:

Animals that are mammal

Cats are mammal

Dogs are mammal

Lions are mammal

In the above example all the classes except the lion class are parent classes and we have used the concept of multilevel inheritance in our program. References of the parent class “Animal” are created that refer to the sub parent classes and through which the methods are called or invoked. In this way we achieved the run time polymorphism with multilevel inheritance.