Polymorphism is one of the core concepts of Object-Oriented Programming (OOP) that allows a single interface or method to perform different tasks. In Java, polymorphism is mainly classified into Compile-time Polymorphism and Run-time Polymorphism, depending on when the method to be executed is determined.
- Improves code reusability and flexibility.
- Supports method overloading and method overriding.
- Makes programs easier to maintain and extend.
Compile-time Polymorphism
Compile-time polymorphism is a type of polymorphism in which the method to be executed is determined during compilation. It is achieved using method overloading, where multiple methods have the same name but different parameters.
- Also known as Static Polymorphism or Early Binding.
- Method selection happens at compile time.
public class GFG {
// First addition function
public static int add(int a, int b)
{
return a + b;
}
// Second addition function
public static double add(
double a, double b)
{
return a + b;
}
// Driver code
public static void main(String args[])
{
// Here, the first addition
// function is called
System.out.println(add(2, 3));
// Here, the second addition
// function is called
System.out.println(add(2.0, 3.0));
}
}
Output
5 5.0
Explanation: In this example, the add() method is overloaded with different parameter types. When add(2, 3) is called, the compiler selects the int version, and when add(2.0, 3.0) is called, it selects the double version. Since the method is chosen during compilation, this is called compile-time polymorphism.
Run-time Polymorphism
Run-time polymorphism is a type of polymorphism in which the method to be executed is determined during program execution. It is achieved using method overriding, where a child class provides its own implementation of a parent class method.
- Also known as Dynamic Polymorphism or Late Binding.
- Method selection happens at runtime by the JVM.
// Implementing a class
class Test {
// Implementing a method
public void method()
{
System.out.println("Method 1");
}
}
// Defining a child class
public class GFG extends Test {
// Overriding the parent method
public void method()
{
System.out.println("Method 2");
}
// Driver code
public static void main(String args[])
{
Test test = new GFG();
test.method();
}
}
Output
Method 2
Explanation: In this example, the method() of the parent class Test is overridden in the child class GFG. Although the reference is of type Test, it points to a GFG object. At runtime, the JVM calls the overridden method() of the child class, printing "Method 2". This is called run-time polymorphism.
Compile-time Vs Run-time Polymorphism
The following table demonstrates the difference between runtime polymorphism and compile-time polymorphism:
| Feature | Compile-time Polymorphism | Run-time Polymorphism |
|---|---|---|
| Also Known As | Static Polymorphism | Dynamic Polymorphism |
| Achieved By | Method Overloading | Method Overriding |
| Binding Time | Compile Time (Early Binding) | Run Time (Late Binding) |
| Method Selection | Decided by the compiler | Decided by the JVM during execution |
| Inheritance Required | No | Yes |
| Method Signature | Same method name with different parameters | Same method signature in parent and child classes |
| Execution Speed | Faster | Slightly slower due to dynamic binding |
| Flexibility | Less flexible | More flexible |
| Decision Based On | Method parameters | Actual object created at runtime |
| Example | add(int, int) and add(double, double) | Parent reference calling overridden child method |
| Use Case | Performing similar operations with different inputs | Providing different implementations of the same method |