Polymorphism is a process in java by which we can use a method in multiple ways or polymorphism to make the method take different forms in java. The child class takes its reference from the parent class. This process is performed either by overloading in java or overriding in java. We use polymorphism when we aim at building an interface with different objects but the same form.
When we create a new object in the program, it takes the form of previous or inherited objects of the parent class via polymorphism. So we can perform many functions in various ways. Just like an animal can have various characteristics at a time. A leopard can be orange in color, ferocious in nature, and striped. As a result, he is so many things at the same time, so this is an example of polymorphism.
Here are some very useful features that we can use polymorphism in Java for:
- It allows the coder to reuse the classes, methods and code again and again.
- We can perform a single function more than one time.
- Even the data types such as int, float, long, double etc. can be used multiple times.
- Simply design one interface and use its objects as many times as you want.
- It allows us to create a code which remains consistent.
Types of Polymorphism in Java

Run time polymorphism: A process It is a process in which a call made to an overridden function is settled at runtime. Dynamic method dispatch is the synonym for run time polymorphism. Below is an example to understand run time polymorphism.
class Tree {
void Print()
{
System.out.println("Roots are the parents of the tree");
}
}
class Plant extends Tree {
void Print()
{
System.out.println("Plants are green");
}
}
class Leaves extends Tree {
void Print()
{
System.out.println("Leaves are fresh");
}
}
class Test{
public static void main(String[] args)
{
Tree t;
t = new Plant();
t.Print();
t = new Leaves();
t.Print();
}
}
The output of this program will be:
Plant
Leaves
So the child classes- plant and leaves will inherit all the features of parent class Root.
Compile-time polymorphism: This type of polymorphism is either achieved by method overloading or operator overloading. Static polymorphism is the synonym for compile-time polymorphism. In this, we can have multiple methods with the same name even if the methods have different parameters, sequences, or data types.
Below is an example of method overloading while performing Polymorphism in java:
class Addnumbers{
static int Add(int x, int y)
{
return x + y;
}
static double Add(double x, double y)
{
return x + y;
}
}
class Main {
public static void main(String[] args)
{
System.out.println(Addnumbers.Add(10, 20));
System.out.println(Addnumbers.Add (20.5, 30.3));
}
}
The output of this program will be:
30
50.8
Now, we will discuss an example of operator overloading in java
class Operator {
void operator(String x, String y)
{
String str = x + y;
System.out.println("The final string will be - "
+ str);
}
void operator(int a, int b)
{
int c = a + b;
System.out.println("Sum = " + c);
}
}
class Main {
public static void main(String[] args)
{
Operator obj = new Operator();
obj.operator(100, 200);
obj.operator("Hello", "Sir");
}
}
The output of the program will be:
300
The final string will be β HelloSir
Hence polymorphism is an object-oriented language concept simple meaning taking more than one form. As the main difference between overloading and overriding in polymorphism is that overriding is done inside different classes whereas overloading is done inside the same class. An object which passes two IS-A relationship test is always polymorphic in nature- one for itself and one for the object class.