Polymorphism explained: one interface, many implementations. How dynamic dispatch picks the right method at runtime and replaces type-checking if-chains.
Polymorphism, explained by cats. Kitty runs a pet show. Every animal must make a sound. Kitty calls: ‘Make a sound!’ One cat meows. Another cat hisses. Another purrs. Same command. Different behavior. That’s polymorphism. In code, Kitty writes one interface.
One method. Like makeSound. Different classes implement differently. Cat meows. Tiger roars. Kitten squeaks. Kitty doesn’t care which animal it is. She just calls the method. The right behavior happens automatically. No if-statements No checking types. Just clean code. That’s polymorphism. one interface, many forms and each cat responding in its own way
Polymorphism lets one piece of code work with many different kinds of object, as long as they all answer the same call. You write animal.makeSound() once, and each animal decides for itself what that means.
One call, many behaviours
The word comes from the Greek for 'many forms'. In programming it means one interface with many implementations behind it. The calling code knows only the interface: which methods exist, what they take and what they return. Each object supplies its own version of those methods.
The print button in an app works the same way. The app sends one print request, and the installed driver, whether for a laser printer or a 'save as PDF' option, works out how to carry it out. The app never needs to know which it is talking to.
A worked example in TypeScript
Start with the contract. An interface says what every animal must be able to do, without saying how:
Polymorphism, explained by cats. Kitty runs a pet show. Every animal must make a sound. Kitty calls: ‘Make a sound!’ One cat meows. Another cat hisses. Another purrs. Same command. Different behavior. That’s polymorphism. In code, Kitty writes one interface.
One method. Like makeSound. Different classes implement differently. Cat meows. Tiger roars. Kitten squeaks. Kitty doesn’t care which animal it is. She just calls the method. The right behavior happens automatically. No if-statements No checking types. Just clean code. That’s polymorphism. one interface, many forms and each cat responding in its own way
The code that runs the show only knows about Animal:
function runShow(animals: Animal[]) { for (const animal of animals) { console.log(`${animal.name}: ${animal.makeSound()}`); }}runShow([new Cat(), new Tiger(), new Kitten()]);// Cat: Meow// Tiger: Roar// Kitten: Squeak
runShow never asks which class it has been given. It calls makeSound() and the right version runs. Add a Lion class next month and runShow works with it unchanged.
How the right method gets picked
When runShow calls animal.makeSound(), the choice of which makeSound to run is made while the program runs, not when it is written. This is called dynamic dispatch. The variable's declared type is Animal, but the object behind it is a real Cat or Tiger, and that object's own class supplies the method.
In Java and C#, each object knows its class, and the runtime looks the method up there, usually through a table of method pointers. In JavaScript and TypeScript, the lookup walks the object's prototype chain. The mechanism differs, but in both cases the object's own class decides which method runs.
The if-statements it replaces
Without polymorphism, code like runShow has to check what kind of animal it has before it can do anything:
function makeSound(animal: { kind: string }) { if (animal.kind === "cat") { return "Meow"; } else if (animal.kind === "tiger") { return "Roar"; } else if (animal.kind === "kitten") { return "Squeak"; } throw new Error("Unknown animal");}
That is fine for three animals. The trouble starts when the same check appears in other places: one function for the sound, another for feeding, another for the enclosure size. Adding a new animal means finding and editing every one of those chains, and missing one is a bug that only shows up at runtime.
With polymorphism, everything about a cat lives in the Cat class. A new animal is a new class, and the code that uses animals doesn't change. This is the open/closed principle in practice: code that is open to extension but closed to modification.
Kinds of polymorphism
What the video shows, one interface with several implementations chosen at runtime, is subtype polymorphism. It is what most people mean by the word in object-oriented code. Two other kinds turn up often:
Parametric polymorphism, better known as generics. One function or class works with any type, such as Array<T> or function first<T>(items: T[]): T. The code is the same for every type; only the type changes.
Ad hoc polymorphism, or overloading. Several functions share a name but take different parameter types, and the compiler picks one from the arguments. Java and C# support method overloading directly.
Duck typing
Dynamic languages such as Python don't need a declared interface at all. If an object has a make_sound method, you can call it, and the language doesn't check which class it came from. The name comes from the saying 'if it walks like a duck and quacks like a duck, it's a duck'.
class Cat: def make_sound(self): return "Meow"class Tiger: def make_sound(self): return "Roar"for animal in [Cat(), Tiger()]: print(animal.make_sound())
TypeScript does something similar when it checks types. Its type system is structural, so any object with a name and a makeSound() that returns a string counts as an Animal, whether or not its class says implements Animal.
Interfaces or inheritance
You can get the same behaviour from a base class whose subclasses override a method. That suits a family of classes that share code: an abstract Animal class with a shared describe() method, say, and an abstract makeSound() that each subclass fills in.
An interface is the lighter choice when classes share a role but no code. It also avoids the deep class trees that inheritance tends to grow, where a change to a base class ripples into every subclass. So reach for an interface first, and keep inheritance for classes that share behaviour.
Polymorphism is also what makes dependency injection work: a class asks for an interface, such as a PaymentGateway, and either the real implementation or a test one is handed in. The decorator pattern relies on it too, wrapping an object in another that implements the same interface.
Common mistakes
Checking the type anyway: writing if (animal instanceof Tiger) inside code that takes an Animal brings back the if-chains polymorphism was meant to remove. If tigers need different handling, that behaviour probably belongs in a method on Tiger.
Breaking the contract: every implementation has to honour what the interface promises. A RubberDuck whose makeSound() throws an error compiles fine but breaks every caller that trusted the interface. This is the Liskov substitution principle: any implementation should work wherever the interface is expected.
Interfaces with only one implementation: an interface for a single class that will never have a sibling adds a file and a layer of indirection for nothing. Add one when there are, or soon will be, several implementations, or when tests need a stand-in.
Interfaces that are too big: an Animal interface with twenty methods forces every class to implement all of them, including ones that don't apply. Smaller interfaces are easier to implement correctly.
Key takeaways
Polymorphism means one interface with many implementations: the caller makes the same call, and each object responds in its own way.
The method that runs is chosen at runtime from the object's actual class, not from the variable's declared type.
It replaces type-checking if-chains, so a new kind of object means a new class rather than edits all over the codebase.
Subtype polymorphism is the usual form in object-oriented code; generics and overloading are other kinds.
Every implementation must keep the interface's promises, or the callers that rely on it break.