What is Polymorphism in Java?

Java is a high-level programming language that follows the OPPs concepts. Polymorphism is one of them. So, “what is Polymorphism”? Polymorphism is a strategy through which we can perform “a single task in many ways”. Polymorphism plays an important role in Java.

The word polymorphism is derived from “poly” and “morphs” words. Both the words are Greek words, which means “many” and “forms”, respectively. So, the means of polymorphism is “many forms”. 

Inheritance and polymorphism work together. The attributes and methods inherit from another class are used by the polymorphism to perform different tasks. A person having different characteristics is a real-life example of polymorphism. A person can be a son, a father, an employee, and a husband at the same time. It defines the correct definition of polymorphism. 

In Java, Polymorphism is classified into two types, i.e., Compile-time and Runtime polymorphism. Let’s dive into deep and learn more about these polymorphisms. 

Compile-time Polymorphism

It is a type of polymorphism that can be achieved by performing function overloading. However, we can also achieve it by performing operator overloading, but operator overloading is not possible in Java. So, we can achieve it only by performing the function overloading. 

In compile-time polymorphism, an object is tied to its functionality at compile-time. It is also known as static or early binding because there is no conflict in the calling of methods. Java checks the method signature and then call it.

Let’s take an example to understand how we can achieve compile-time polymorphism in Java:

CompileTimePolymorphism.java

Explanation:

In the above example, we have created a class with the name CompileTimePolymorphism and defined the two methods inside it with the same name but with different signatures. In the first method, we concatenate the two strings and assign them to the third variable str3. After that, we have shown the concatenate result to the user. 

In the second method, we add two integer values and assign the result to the third integer variable result. After assigning it, we have shown the result to the user. In the main() method, we create two string variables. i.e., str1 and str2. We assign default string values to both of these variables. After that, we call both the showData() method by passing appropriate values to the methods. 

Output:

Runtime Polymorphism

Runtime Polymorphism is another type of polymorphism. It is also known as Dynamic Method Dispatch or Late Binding. It is a process or mechanism in which the overridden method’s call is solved at runtime. For understanding runtime polymorphism, we should have knowledge of Overriding in Java and Inheritance. We achieve Runtime Polymorphism by performing Function Overriding or Method Overriding

  1. Java determines the version of the method, which is to be executed at the time of calling the overridden method through a superclass reference. The determination is based on the object type referred to at the time the call occurs.
  2. At runtime, it doesn’t depend on the reference variable’s type. It only depends on the object type, which is going to be referred to. 
  3. In runtime polymorphism, Upcasting is used to solve the overridden method’s call at runtime. 

Let’s take an example to understand how we can achieve runtime polymorphism in Java:

TestLivingThings.java

Explanation:

In the above code, we create four classes, i.e., parent class, two-child classes and class that contains the main() method. The parent and both the child classes have a move() method to print different sentences. In the main class, we create three objects, i.e., l1, l2, and l3

The l1 object is of type LivingThings, and the reference is passed of the same class to that object. So, this object will call the move() method of the LivingThings class and show the message “Living things breath”.

The L2 object is of type Plant, and the reference is passed to the LivingThings class. So, this object will call the move() method of the Plant class and show the message “Plants moves in response to another”.

The L3 object is of type Person, and the reference is passed to the LivingThings class. So, this object will call the move() method of the Person class and show the message “Person moves from one place to another”.

Compile-time Polymorphism Vs Runtime Polymorphism

Below is a table which illustrate the difference between both of them.

S.NoCompile-time PolymorphismRuntime Polymorphism
In compile-time polymorphism, the method call is solved at compile-time.In runtime polymorphism, the method call is solved at runtime.
This compile-time polymorphism is achieved by performing the function overloading.The runtime polymorphism is achieved by performing the function overriding.
It is also known as early binding. It is also known as dynamic binding or late binding.
In this polymorphism, two or more methods have the same name, but return types and signatures are different.In this polymorphism, two or more methods have the same method name, same parameters and same return type. But all the methods reside in different classes.
Compile-time polymorphism gives fast execution. Runtime polymorphism gives slow execution in comparison to compile-time polymorphism. 
It makes this polymorphism less flexible.It makes this polymorphism more flexible.