If JavaScript objects were actors, polymorphism would be their superpower ๐ญ
It allows the same function name to behave differently depending on whoโs calling it.
Think of saying โrunโ:
- A child runs playfully ๐ถ
- A sprinter runs fast ๐
- A computer program runs your code ๐ฅ๏ธ
Same word. Different behavior.
Thatโs exactly what polymorphism means in JavaScript!
Letโs explore this in the most beginner-friendly way possible ๐
๐ What Is Polymorphism?
Polymorphism = One interface, multiple forms
In simpler words:
๐ฃ๏ธ You call the same method, but each object executes it in its own way.
JavaScript uses polymorphism mainly through:
- Method overriding (most common)
- Method overloading (JS doesnโt have true overloading, but we mimic it)
๐พ Classic Example: Animals Making Sounds
class Animal {
makeSound() {
console.log("Some sound...");
}
}
class Dog extends Animal {
makeSound() {
console.log("Woof!๐ถ");
}
}
class Cat extends Animal {
makeSound() {
console.log("Meow!๐ฑ");
}
}
const animals = [new Animal(), new Dog(), new Cat()];
animals.forEach(a => a.makeSound());
Output:
Some sound...
Woof!๐ถ
Meow!๐ฑ
Same method โ makeSound()
Different behavior โ based on class โ
๐ฎ Real-Life Example: Payment System
class Payment {
pay() {
console.log("Processing payment...");
}
}
class CreditCard extends Payment {
pay() {
console.log("Paid using Credit Card ๐ณ");
}
}
class UPI extends Payment {
pay() {
console.log("Paid using UPI ๐ฑ");
}
}
function checkout(paymentMethod) {
paymentMethod.pay();
}
checkout(new UPI());
checkout(new CreditCard());
You can plug in any payment method
and pay() will run the correct implementation.
That’s polymorphism in action โ flexible, clean, future-friendly.
๐ Another Example: Vehicles Move Differently
class Vehicle {
move() {
console.log("The vehicle is moving...");
}
}
class Car extends Vehicle {
move() {
console.log("Car is driving on the road ๐");
}
}
class Plane extends Vehicle {
move() {
console.log("Plane is flying in the sky โ๏ธ");
}
}
const v = [new Car(), new Plane()];
v.forEach(item => item.move());
When you call move(), each vehicle responds in its unique way.
๐ Why Use Polymorphism?
โ Cleaner code
โ Easier to extend
โ Reduces duplicate logic
โ Makes systems flexible
โ Perfect for plug-and-play behaviour
๐ก When Is Polymorphism Useful?
Whenever you have:
- Multiple objects sharing an action
- Each object needs its own version of that action
- You want to simplify function calls
Examples:
- Notifications (email, SMS, push)
- Rendering UI widgets
- Payment gateways
- Vehicles / animals / game characters
- API handlers
๐ Final Thoughts
Polymorphism is not as scary as it sounds.
It’s simply:
โSame method name, different behavior.โ
Once you understand this idea, JavaScript classes feel more intuitive, flexible, and clean.
Use it to build smarter, more scalable apps!

Leave a Reply