Java Polymorphism
Java polymorphism is one of the four main principles of Object-Oriented Programming (OOP). The word polymorphism means "many forms". In Java, polymorphism allows a single method or object to perform different tasks depending on the situation. It makes programs more flexible, reusable, and easier to maintain.
Java supports two types of polymorphism: Compile-time Polymorphism (Method Overloading) and Runtime Polymorphism (Method Overriding).
What is Polymorphism in Java?
Polymorphism is the ability of an object to take many forms. A parent class reference can point to different child class objects, and the appropriate method is executed based on the actual object at runtime.
Types of Polymorphism in Java
Compile-Time Polymorphism
Achieved through method overloading. The compiler decides which method to call.
Runtime Polymorphism
Achieved through method overriding. The JVM decides which method to execute during runtime.
Compile-Time Polymorphism (Method Overloading)
Method overloading occurs when multiple methods have the same name but different parameter lists. The compiler determines which method to execute based on the arguments passed.
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main{
public static void main(String[] args) {
Calculator obj = new Calculator();
System.out.println(obj.add(10, 20));
System.out.println(obj.add(10, 20, 30));
}
}
Runtime Polymorphism (Method Overriding)
Runtime polymorphism is achieved through method overriding. A child class provides its own implementation of a method already defined in the parent class.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal obj = new Dog();
obj.sound();
}
}
Advantages of Polymorphism
- It improves code reusability.
- It increases application flexibility.
- It supports the dynamic method dispatch.
- It reduces the code duplication.
- it makes the programs easier to maintain.
- It supports the scalable object-oriented design.
Real-World Example of Polymorphism
Consider a payment application that supports multiple payment methods such as
Credit Card, UPI, and Net Banking. Each payment class implements its own
pay() method. The application simply calls the pay()
method without worrying about the actual payment type. This is runtime polymorphism.
Method Overloading vs Method Overriding
| Method Overloading | Method Overriding |
|---|---|
| Compile-time polymorphism. | Runtime polymorphism. |
| Same method name with different parameters. | Same method signature in parent and child classes. |
| Occurs within the same class. | Occurs between parent and child classes. |
| Resolved by the compiler. | Resolved by the JVM during execution. |
Important Points
- Polymorphism means "many forms."
- Method overloading provides compile-time polymorphism.
- Method overriding provides runtime polymorphism.
- Runtime polymorphism requires inheritance.
- Dynamic method dispatch is possible because of runtime polymorphism.
Summary
Java polymorphism enables a single interface to represent multiple forms of behavior. It is one of the most powerful OOP concepts because it makes applications flexible, reusable, and easier to extend. Java supports compile-time polymorphism through method overloading and runtime polymorphism through method overriding. Understanding polymorphism is essential for building scalable and maintainable Java applications.