Table of Contents
What is Polymorphism? In simple words, it means “Many Forms”. What this means in the object-oriented programming language like Java is that, a single line of code (Here, a single line of code can refer to the name of a method, or a variable, etc.) can have multiple meanings, and which meaning is to be used depends upon various factors.
Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding. For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time. Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.
Note: The print () method is also an example of polymorphism. It is used to print values of different types like char, int, string, etc. We can achieve polymorphism in Java using the following ways: During inheritance in Java, if the same method is present in both the superclass and the subclass.
Polymorphism in Java
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.
There are two types of polymorphism in Java: compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by method overloading and method overriding.
If you overload a static method in Java, it is the example of compile time polymorphism.
Learn to code from industry experts! Enroll here
Types of Polymorphism
Java supports two types of polymorphism namely:
- Compile-time polymorphism
- Run-time polymorphism.
1. Compile-time Polymorphism:
Compile-time polymorphism is the type of polymorphism occurs when the compiler compiles a program. Java supports compile-time polymorphism through method overloading. The other names of Compile-time polymorphism are static polymorphism or early binding.
Polymorphism occurs in method overloading because method overloading allows access to different methods through the same interface.
In method overloading, two or more methods in a class can use the same name as long as their parameter declarations are different.
When the compiler encounters a call to an overloaded method, it identifies the correct version of the overloaded method by comparing the type and the number of arguments.
Example:
int value : 10
String value : online tutorials point
Name with value : 20 Overloading Demo
2. Run-time Polymorphism:
Run-time Polymorphism is the type of polymorphism that occurs dynamically when a program executes. Java supports run-time polymorphism by dynamically dispatching methods at run time through method overriding.
For this type of polymorphism, method invocations are resolved at run time by the JVM and not at the compile time.
Since the run-time polymorphism occurs dynamically, it is also called dynamic polymorphism or late binding.
interface Car {
void speed(int speed);
}
class Maruti implements Car {
public void speed(int speed) {
System.out.println("Maruti Speed is :" + speed);
}
}
class Tata implements Car {
public void speed(int speed) {
System.out.println("Tata Speed is :" + speed);
}
}
public class PolymorphismDemo {
public static void main(String[] args) {
Car car;
car = new Maruti();
car.speed(200);
car = new Tata();
car.speed(300);
}
}
Output:
In the above example – Maruthi and Tata are two different classes, and both were two different implementations of the Car Interface. In other words, these two classes are types of Car.
Since because these two classes are the same type, we can refer these two objects to a single type called Car as above. Here, based on the type of the object being assigned with the reference, polymorphism is resolved at runtime.
Grab the opportunity to learn Java with Entri! Click Here
Usages and Advantages of Polymorphism
-
Method overloading allows methods that perform similar or closely related functions to be accessed through a common name. For instance, a program performs operations on an array of numbers which can be int, float, or double type. Method overloading allows you to define three methods with the same name and different types of parameters to handle the array of operations.
-
Constructors allowing different ways to initialize objects of a class can implement method overloading. This enables you to define multiple constructors for handling different types of initializations.
-
Method overriding allows a subclass to use all the general definitions that a superclass provides and add specialized definitions through overridden methods.
-
Method overriding works together with inheritance to enable code-reuse of existing classes without the need for re-compilation.
Differences Between Compile-time and Run-time Polymorphism
-
Method overloading occurs at Compile-time whereas method overriding occurs at Run-time.
-
Method overloading occurs when the type signatures of the two methods are different whereas method overriding occurs only when the type signatures of the two methods are the same.
-
In method overloading, the compiler calls the correct method by comparing the type signatures. However, in method overriding, the JVM determines the correct method based on the object that the invoking variable is referring to.
-
Method overloading is possible on methods with private, static, and final access modifiers. On the other hand, method overriding is not possible on these access modifiers.