🔥 Polymorphism in Java – The Superpower of OOP!
Imagine a single word with many meanings — like “run”.
🏃 You can run in a park.
💻 A program can run on a computer.
⚡ Electricity can run through wires.
That’s exactly what Polymorphism means in Java — one action, multiple forms!
🎯 What is Polymorphism?
👉 The word Polymorphism comes from Greek: “poly” = many, “morph” = forms.
👉 In Java, polymorphism allows an object to behave differently based on context.
👉 It makes code flexible, reusable, and easier to maintain.
⚡ Types of Polymorphism in Java
1️⃣ Compile-time Polymorphism (Method Overloading)
-
Achieved by defining multiple methods with the same name but different parameters.
-
Decided at compile-time.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(5, 10)); // Calls int method
System.out.println(c.add(5.5, 10.5)); // Calls double method
}
}
Output:
15
16.0
👉 Same method name add() but different forms.
2️⃣ Runtime Polymorphism (Method Overriding)
-
Achieved when a subclass provides its own implementation of a method already defined in the parent class.
-
Decided at runtime using dynamic method dispatch.
class Animal {
void sound() {
System.out.println("Animal makes a 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 Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Dog barks
a = new Cat();
a.sound(); // Cat meows
}
}
Output:
Dog barks
Cat meows
👉 Same method sound() but different behaviors depending on the object.
More Examples:
⚡ 1. Method Overloading (Compile-time Polymorphism)
👉 Same method name, but different parameters (number or type).
class Calculator {
// Method with 2 integer parameters
int add(int a, int b) {
return a + b;
}
// Method with 3 integer parameters
int add(int a, int b, int c) {
return a + b + c;
}
// Method with 2 double parameters
double add(double a, double b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
Calculator calc = new Calculator();
System.out.println(calc.add(10, 20)); // Calls int add(int,int)
System.out.println(calc.add(10, 20, 30)); // Calls int add(int,int,int)
System.out.println(calc.add(5.5, 4.5)); // Calls double add(double,double)
}
}
Output:
30
60
10.0
👉 Here, the method add() has multiple forms, decided at compile-time.
⚡ 2. Method Overriding (Runtime Polymorphism)
👉 Subclass provides a new 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");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a;
a = new Dog();
a.sound(); // Calls Dog's sound()
a = new Cat();
a.sound(); // Calls Cat's sound()
}
}
Output:
Dog barks
Cat meows
👉 Here, the method sound() is overridden and resolved at runtime.
✨ Quick Difference:
-
Overloading → Same method name, different parameter list.
-
Overriding → Same method name & parameters, but different implementation in subclass.
Would you like me to also prepare a side-by-side code infographic (Overloading vs Overriding) for your website?
💡 Real-World Example of Polymorphism
Think of a remote control 🎛️
-
The same button
power()can turn TV ON/OFF, AC ON/OFF, or Music System ON/OFF depending on the device. -
That’s polymorphism in action!
⚡ Why Use Polymorphism?
✅ Improves code reusability
✅ Increases flexibility
✅ Makes maintenance easier
✅ Supports dynamic behavior
🎯 Quick Recap:
-
Method Overloading (Compile-time) → Same name, different parameters.
-
Method Overriding (Runtime) → Same method, different implementations.
🔑 Difference Between Method Overloading and Method Overriding in Java
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Definition | Same method name but different parameter list (number/type). | Subclass provides its own implementation of a method already defined in the parent class. |
| Polymorphism Type | Compile-time (Static) Polymorphism | Runtime (Dynamic) Polymorphism |
| Parameters | Must be different (in number, type, or order). | Must be exactly the same (name, parameters, return type). |
| Return Type | Can be same or different (but should be compatible). | Must be same or covariant (subtype). |
| Inheritance | Not required (can happen in the same class). | Requires inheritance (must involve parent-child relationship). |
| Access Modifiers | Can have any access modifier. | Cannot reduce visibility (e.g., public in parent cannot become private in child). |
| Annotations | @Override not used. |
Commonly uses @Override annotation. |
| Example | int add(int a, int b) vs double add(double a, double b) |
Animal.sound() overridden by Dog.sound() |