Polymorphism In Java With Example
Polymorphism means “many forms”. Polymorphism means one name and many duties.
That is, the same entity (method or operator or object) can perform different operations in different scenarios.
Real-life Illustration of Polymorphism in Java: A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, and an employee. So the same person possesses different behaviors in different situations. This is called polymorphism.
![](https://msblab.com/wp-content/uploads/2023/11/download.png)
![](https://msblab.com/wp-content/uploads/2023/11/Polymorphism-In-Java.png)
Polymorphism can be defined as the ability to use the same for two or more related but technically different tasks. In other words, polymorphism can be defined as interface that can be used to perform related but different tasks.
Types of Polymorphism
There are two types of polymorphism:
- Compile time polymorphism / static polymorphism (Method overloading )
- Run time polymorphism / Dynamic Method Dispatch
Compile time polymorphism
It is also known as static polymorphism. This type of polymorphism is achieved by function overloading or operator overloading (But Java doesn’t support the Operator Overloading.)
![](https://msblab.com/wp-content/uploads/2023/11/Capture-1.png)
Whenever an object is bound with their functionality at compile time this is known as compile time polymorphism. At compile time, java knows which method to call by checking the method signatures. So this is called compile time polymorphism or static or early binding.
Method Overloading
When there are multiple functions with the same name but different parameters then these functions are said to be overloaded. Functions can be overloaded by changes in the number of arguments or/and a change in the type of arguments. In java we achieve function overloading at run time.
Program Example of Compile time polymorphism:
Let us take an example to show the working of compile time polymorphism through function overloading.
Step 1: First we create a class Addition in which we declare three add() methods with different parameters:
class Addition { public int add(int m,int n) { return m+n; } public int add(int m,int n,int o) { return m+n+o; } public int add(int m,double n) { return m+(int)n; } }
Step 2: Second we create a class PrintAdd in which we call the above class methods by creating an object:
class PrintAdd { public static void main(String args[]) { Addition a=new Addition(); System.out.println(a.add(2,3)); System.out.println(a.add(2,3,4)); System.out.println(a.add(2,3.4)); } }
Output:
5 9 5
Explanation of Example:
In above example there are three versions of add methods. The first method takes two parameters or arguments while the second one takes three parameters. In the third method there is change in type of parameters. The compiler looks at the method signature and decides which method to call for a particular method call at compile time.
Run Time Polymorphism (dynamic or late binding)
Run time or dynamic polymorphism is achieved(implemented) through method overriding.
Method overriding says child class has the same method as declared in the parent class.
What is Method Overriding?
Declaring a method in subclass, which is already present in superclass is known as Method Overriding.
Why do we need Method Overriding?
- Code reuse
- One interface, multiple implement
- Run time polymorphism.
It means if child class provides the specific implementation of the method that has been provided by one of its parent class then it is known as method overriding.
What are the rules for Method Overriding?
- Name, signature type, parameter must be same
- If a method can’t be inherited, then it can’t be overridden.
- A method declared as final or static can’t be overridden.
- Constructor can’t be overridden.
Program Example of Run time polymorphism:
Let us take an example to show the working of run time polymorphism through method overriding.
Step 1: First we create a class Vehicle in which we declare a move() method:
class Vehicle { public void move() { System.out.println("vehicles can move"); } }
Step 2: Second we create a class Bike which extends the class Vehicle:
class Bike extends Vehicle { public void move() { System.out.println("bike can move and accelerate too"); } }
Step 3: Third we create a class TestVehicle in which we create an object and call the above class method:
class TestVehicle { public static void main(String args[]) { Vehicle v = new Bike(); v.move(); Vehicle v1 = new Vehicle(); v1.move(); } }
Output:
Bike can move and accelerate too Vehicles can move
Explanation:
In above example, it should be noted that in the first call to move(),the reference type is Vehicle and the object is being referenced is Bike. So when a call to move() is made, java waits until runtime to check which object is being pointed to by reference. In this case,the object is of class Bike. So the move() method of Bike class will be called. In the second call to move(),the object is of class Vehicle. So the move() method of Vehicle will be called.
// Java Program for Method Overriding // Class 1 // Helper class class Parent { // Method of parent class void Print() { // Print statement System.out.println("parent class"); } } // Class 2 // Helper class class subclass1 extends Parent { // Method void Print() { System.out.println("subclass1"); } } // Class 3 // Helper class class subclass2 extends Parent { // Method void Print() { // Print statement System.out.println("subclass2"); } } // Class 4 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of class 1 Parent a; // Now we will be calling print methods // inside main() method a = new subclass1(); a.Print(); a = new subclass2(); a.Print(); } }
Output
subclass1 subclass2
Explanation of the above code:
Here in this program, When an object of a child class is created, then the method inside the child class is called.
This is because The method in the parent class is overridden by the child class.
Since The method is overridden, This method has more priority than the parent method inside the child class. So, the body inside the child class is executed.
Method overloading vs Method overriding
Method overloading | Method overriding |
---|---|
Method overloading occurs in a single class | Method overloading occurs in a different class |
Method parameter must be different | Method parameter must be same |
No inheritance needed. | Must be inherited |
Return type may be different | Method Return type must be same |
One method does not hide another | Child method hides parent method |