Polymorphism In Java With Example Programs
By AmarSivas | | Updated : 2021-11-02 | Viewed : 211 times

Polymorphism is one of the main features of OOPs. It is very important to know the basics of Polymorphism in java. So we will try to explore the basics of Polymorphism.
Table of Contents:
What Is Polymorphism?
Polymorphism can be defined by an object's ability. Polymorphism allows for performing the actions in different ways. In another way to define polymorphism, it allows to Object to which method to be selected for execution.
What Is Polymorphism In Java?
We will first div into the topic of Static and dynamic polymorphism.
Types Of Polymorphism
We will first div into the topic of types of Polymorphism. We have two types of Polymorphism. One is Static and the second is dynamic polymorphism.
Static Polymorphism
Static Polymorphism is also called early binding, A call from an object to the method can be resolved by the compiler in compile time. So that's why this type of Polymorphism is called Static Polymorphism. An example of Static Polymorphism is overloading.
Dynamic Polymorphism
Now we will look at Method Overriding and Method Overloading with examples.
Method Overriding In Java
Method overriding means the method declaration of the subclass is the same as the superclass but the method body will be different in the subclass. The method body is overridden in a subclass of the superclass.
Please refer to the given example for method overriding in java.
public class Vehicle {
public void run(){
System.out.println("Vehicle is running");
}
}
public class Car extends Vehicle {
public void run(){
System.out.println("Car is running");
}
}
Notice method resolution depends upon object reference. So If you use subclass's object then the subclass method will be invoked.
Method Overloading In Java
Method overloading means the same type of method may take any number of parameters. Then this type of method is called as the method is overridden. It is early binding means method resolution decided by the compiler in compile time. Please refer to the below given example for method overloading in java.
package com.docsconsole.tutorials.overloading;
public class Vehicle {
public void run() {
System.out.println("Vehicle is running");
}
}
package com.docsconsole.tutorials.overloading;
public class Car extends Vehicle {
public void run() {
System.out.println("Car is running");
}
public void run(String brand) {
System.out.println("Car is running and brand:"+brand);
}
}
public class MainClient {
public static void main(String[] args) {
Car car = new Car();
car.run("audi");
}
}
When you execute the main client you can see the Car method will be executed. As you know it is early binding, call resolution will decide at compile time. So Car's method will be executed.
Why Do We Need Polymorphism In Java?
Polymorphism allows Objects to perform a single action in multiple ways. Consider overriding when calling the same type of method it will be invoked based on the object type. so here one method will be having different types of forms which is nothing but Polymorphism. If you use a subclass object then the subclass's method will be invoked. If you use a superclass object then you can invoke the superclass method. The same method can be invoked and used in different ways. This is called polymorphism.
Advantages Of Polymorphism
Advantages Of Polymorphism
-
It will be applicable to methods in Java for performing single action in different ways.
-
It allows an object to decide to call the method in runtime.