Skip to content
AgileLogics
4 min read

Polymorphism

Prerequisites: Inheritance

Polymorphism means different classes can define a method with the same name, and calling code can use any of them the same way, without caring which specific class it actually is.

class Dog {
    speak() { return "Woof"; }
}

class Cat {
    speak() { return "Meow"; }
}

for (const animal of [new Dog(), new Cat()]) {
    console.log(animal.speak());
}

Expected output

Woof
Meow