Polymorphism in Java
Polymorphism is one of the four main principles of Object-Oriented Programming (OOP). The word polymorphism means "many forms."
In Java, polymorphism allows the same method name or interface to perform different behaviors depending on the situation.
There are two main types of polymorphism:
- Compile-Time Polymorphism
- Runtime Polymorphism
1. Compile-Time Polymorphism
Compile-time polymorphism is achieved through method overloading.
Method overloading means having multiple methods with the same name but different parameters in the same class.
Example
class Calculator { int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } double add(double a, double b) { return a + b; } public static void main(String[] args) { Calculator c = new Calculator(); System.out.println(c.add(10, 20)); System.out.println(c.add(10, 20, 30)); System.out.println(c.add(10.5, 20.5)); } }
Output
30 60 31.0
The compiler determines which add() method should be called based on the arguments.
2. Runtime Polymorphism
Runtime polymorphism is achieved through method overriding.
Method overriding occurs when a child class provides its own implementation of a method that is already defined in the parent class.
Example
class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } } class Main { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }
Output
Dog barks
Here, the reference type is Animal, but the actual object is Dog. Therefore, the overridden sound() method of Dog is executed at runtime.
Difference Between Compile-Time and Runtime Polymorphism
| Compile-Time Polymorphism | Runtime Polymorphism |
|---|---|
| Achieved through method overloading. | Achieved through method overriding. |
| Method is selected at compile time. | Method is selected at runtime. |
| Usually occurs within the same class. | Requires inheritance. |
| Parameters must be different. | Method signature is generally the same. |
| Also called static polymorphism. | Also called dynamic polymorphism. |
| Example: Method overloading. | Example: Method overriding. |
Advantages of Polymorphism
- Flexibility – One interface can support different behaviors.
- Code Reusability – Reduces unnecessary duplicate code.
- Maintainability – Makes programs easier to modify.
- Extensibility – New classes and behaviors can be added easily.
- Supports Loose Coupling – Code can work with general parent types or interfaces.
- Improves Readability – Similar operations can use the same method name.
Real-Life Example
Consider a Shape class with a draw() method.
Different shapes can implement draw() differently:
Shape ├── Circle → draw() ├── Rectangle → draw() └── Triangle → draw()
The same draw() method can produce different behavior depending on the object.
Key Points
- Polymorphism means many forms.
- It is an important concept of Java OOP.
- Method overloading provides compile-time polymorphism.
- Method overriding provides runtime polymorphism.
- Runtime polymorphism requires inheritance or interfaces.
- The
@Overrideannotation is commonly used when overriding a method.
Conclusion
Polymorphism allows Java programs to use a common method name or interface for different behaviors. It makes programs more flexible, reusable, maintainable, and easier to extend.