Polymorphism in Java

Here is the table content of the article will we will cover this topic.
1. What is Polymorphism in JAVA?
2. Static polymorphism or compile-time polymorphism
3. Method overloading
4. An important concept of method Overloading

5. Overloading of Static method
6. Overloading of the main method
7. Covariant return type in java
8. Dynamic polymorphism or runtime polymorphism
9. Method overriding

10. When to use Polymorphism?

Polymorphism in Java is a concept by which we can perform a single action in different ways. Polymorphism is derived from 2 greek words: poly and morphs. The word “poly” means many, and “morphs” means forms. So, Polymorphism means many forms.

Real-life example: You have a smartphone, and you can do communicate with your friend. The communication mode you choose could be anything. You can do a call, a text message, mail, etc. So, there is a common goal that is communication, but their approach is different. Therefore, It is called Polymorphism.

Polymorphism in Java

What is Polymorphism in JAVA?

Polymorphism is the OOPs feature that allows us to perform a single action in different ways. In JAVA, we can perform different tasks by a single action.

For example: Let’s say we have two cars both belong to vehicles. All vehicles have some common properties. All Vehicles have their own speed, power, color, etc. Since this is a common thing in Vehicles, but each vehicle behaves differently. So, we can take it generic (Common) class.

class Vehicles
{
	public void detailsOfVehicle()
	{
		System.out.println("This is generic class of vehicles and each vehicle has 
                some speed, power etc");
	}
}

class Swift extends Vehicles
{
	@Override
	public void detailsOfVehicle()
	{
		System.out.println("Its top speed it 200km");
		System.out.println("Its power speed it 1200cc");
	}
}

class HondaCity extends Vehicles
{
	@Override
	public void detailsOfVehicle()
	{
		System.out.println("Its top speed it 300km");
		System.out.println("Its power speed it 1800cc");
	}
}

class MainClass
{
	public static void main(String arg[])
	{
		// Creating a object of Swift class call and assign to refernce variables of 
                // vehicles.
		Vehicles vehicles = new Swift();
		vehicles.detailsOfVehicle();
		
		// Creating a object of HondaCity call and assign to refernce variables of 
                // vehicles.
		vehicles = new HondaCity();
		vehicles.detailsOfVehicle();
				
	}
}

Output: Its top speed is 200km
Its power speed is 1200cc
It’s top speed is 300km
Its power speed is 1800cc

In the above example, we created three classes. One class is vehicles that hold common methods (generic features of Vehicles). Also, The other two classes are extending the Vehicles class and override the method detailsOfVehicle. Both classes, overriding the method according to the features.
As you can see, we have common action detailsOfVehicle() in Vehicles class. The common action is overridden by all subclasses so that each class behaves differently. It is a perfect example of polymorphism because it performs a single action in different ways.

Types of Polymorphism in Java

  1. Static polymorphism/compile-time polymorphism
  2. Dynamic polymorphism/runtime polymorphism

1. Static polymorphism or compile time polymorphism

Static polymorphism resolve as a compile-time. Also, this Static Polymorphism can achieve by method overloading. First, You should read about the method overloading and how many ways to overload a method. If you have read the method overloading, then you can read further.

Method overloading

Method overloading means multiple methods having the same name but different parameters. A class can hold several methods having the same name, but different types/order/number of parameters.

Let’s discuss with a scenario:
Suppose you want to perform the addition of the given numbers. But users can enter any number of arguments.

The problem without overloading: If you write the method addTwo(int, int) for two parameters, and addThree(int,int,int) for three parameters, then it may be difficult for you as well other programmers to understand the behavior of the method. It may be difficult to track how many methods in the class. That is performing addition because each method has a different name.

For more details of method overloading, you can go through the link.
Solution by method overloading: We can create both methods with the same name but with different numbers of parameters. Let’s create two methods, the first is addition(int, int), and the second is addition(int,int,int).

polymorphism java - overloading method

At compile time, Java knows which method to invoke by checking the method signatures.

Class AdditionsOfNumber
{
	public void addition(int a, int b)
	{
		System.out.println("Addition of two numbers = "+ (a+b));
	}
	public void addition(int a, int b, int c)
	{
		System.out.println("Addition of three numbers = "+ (a+b+c));
	}	
}

class ExampleOfStaticPolymorpism
{
	public static void main(String arg[])
	{
		AdditionsOfNumber addNumbers = new AdditionsOfNumber();
		// calling the method have two parameters
		addNumbers.addition(2,3);
		// calling the method have three parameters
		addNumbers.addition(2, 4, 5);
	}
}

Output: Addition of two numbers = 5
Addition of three numbers = 11

2. Dynamic polymorphism or runtime polymorphism

Dynamic Polymorphism is a process resolved at runtime. Dynamic Polymorphism can achieve by method overriding. You should read about the method overriding and What are the rules to override a method. If you have read the method overloading, then you can read further.

Method overriding

If parent class and child class have a method with the same signatures? Then its call method overriding. Let’s say a parent class has a method sum(int, int), and a child class provides the specific implementation of the method that has been declared in a parent class. Hence, It is known as method overriding.

Finally, For more details of method overloading, You can go through the link.

class Additions
{
	public void addition(int a, int b)
	{
		System.out.println("Method of Parent(Additions) class and Addition of two 
                numbers = "+ (a+b));
	}
}

class ExampleOfDynamicPolymorpism extends Additions
{
	// Override the method of parent class (Additions)
	public void addition(int a, int b)
	{
		System.out.println("Method of child(ExampleOfDynamicPolymorpism) class and 
                 Addition of two numbers = "+ (a+b));
	}
	
	public static void main(String arg[])
	{
		Additions addNumbers = new Additions();
		addNumbers.addition(2,3);
	}
}

Output: Method of Parent(Additions) class and Addition of two numbers = 5

Rules:

  1. The method must have the same name as in the parent class
  2. The method must have the same parameter as in the parent class.
  3. The method must have the same return type as in the parent class
  4. There must be an IS-A relationship (inheritance).

Leave a Comment