Skip to content

Polymorphism

In one line

Polymorphism means the same operation can behave differently depending on the type of object it is applied to.

In simple words

Polymorphism means "many forms." In programming, it means you can use the same method name or operator on different types of objects, and each one responds in its own way. The calling code does not need to know which specific type it is dealing with.

For example, a speak() method on a Dog returns "Woof" and on a Cat returns "Meow." You can loop over a list of animals and call speak() on each — each one makes its own sound. The loop does not need to know whether each animal is a dog or cat.

Polymorphism simplifies code: instead of writing if (animal is Dog) woof(); else if (animal is Cat) meow();, you just call animal.speak() and each type handles it its own way. This is a core principle of object-oriented programming.

Example

javascript
class Dog { speak() { return "Woof"; } }
class Cat { speak() { return "Meow"; } }
class Cow { speak() { return "Moo"; } }

const animals = [new Dog(), new Cat(), new Cow()];
animals.forEach(a => console.log(a.speak()));
// Woof, Meow, Moo

Each animal type has its own speak() method. The loop calls speak() on each without knowing the specific type — polymorphism handles the difference.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary