Polymorphism
In Java, polymorphism is one of the four core principles of object-oriented programming (OOP), alongside inheritance, encapsulation, and abstraction. At its core, polymorphism allows objects to take many forms depending on the context. This dynamic nature leads to more flexible and maintainable code.
There are two main types of polymorphism in Java:
- Compile-time Polymorphism (also known as method overloading)
- Runtime Polymorphism (also known as method overriding)
1. Compile-time Polymorphism (Method Overloading)
Compile-time polymorphism occurs when multiple methods in the same class have the same name but different parameter lists. The decision about which method to call is made by the compiler.
Example: Method Overloading
public class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(2, 3));
System.out.println(calc.add(2.5, 3.5));
System.out.println(calc.add(1, 2, 3));
}
}
5
6.0
6
Explanation:
Here, the method add is overloaded three times. The compiler picks the correct version to invoke based on the number and types of arguments. This helps in writing intuitive APIs while avoiding redundant method names.
2. Runtime Polymorphism (Method Overriding)
Runtime polymorphism happens when a subclass provides a specific implementation of a method that is already defined in its superclass. The method that gets executed is determined at runtime based on the object's actual type.
Example: Method Overriding
class Animal {
void sound() {
System.out.println("Some generic animal sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void sound() {
System.out.println("Cat meows");
}
}
public class TestPolymorphism {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound();
a = new Cat();
a.sound();
}
}
Dog barks
Cat meows
Explanation:
Even though the reference variable a is of type Animal, the actual object it points to is either a Dog or a Cat. At runtime, Java determines the real object and invokes the appropriate sound() method. This is known as dynamic method dispatch.
Polymorphism in Action: Real-World Analogy
Imagine you have a remote control. Whether it operates a TV, a fan, or an AC depends on the device it’s currently linked to. The same remote (reference) can behave differently (method call) depending on the actual device (object instance) — this is polymorphism in everyday life.
Conclusion
Java Polymorphism isn’t just a concept—it’s a paradigm shift in how we design programs. With method overloading, you get cleaner, more intuitive code. With method overriding, you get flexibility and adaptability.

About the authorMallikarjuna Mallisetty
General programming
Mallikarjuna shares practical programming tutorials and foundational concepts designed to help developers learn by building and experimenting.
View LinkedIn profile ↗