Understanding Polymorphism in JavaScript

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!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *