Polymorphism
One question, “what is your area?”, asked of a circle, a square and a triangle. Each answers in its own way, the caller never checks which is which, and a chain of ifs disappears.
In 30 seconds
Polymorphism lets code talk to many kinds of object through one shared interface. A program asks any Shape for its area; each kind of shape has its own way of working it out.
Each class overrides the method with its own version, and at run time the language runs the version belonging to the actual object. That choice is called dynamic dispatch.
The gain is that the calling code needs no “if it is a circle, else if it is a square” chain, and a new kind can be added without editing the code that uses it.
Polymorphism, step by step
The code is Java. This is subtype polymorphism; generics and duck typing are related forms. Overloading rules vary: JavaScript and Python have no overloading of this kind.
- Step 1: The if-chain problem. Suppose a program handles circles, squares and triangles, and needs the area of each. The obvious code asks “what kind are you?” and branches. It works, but every function that treats shapes differently needs its own chain, and each new kind of shape means finding and editing all of them.
- Step 2: One interface. Polymorphism starts with an agreement. A common type, Shape, declares the one thing every shape must be able to do: report its area. Circle, Square and Triangle are each declared to be a kind of Shape. Code that holds “some Shape” can now ask for its area without knowing which kind it is.
- Step 3: Each class overrides area(). Each subclass supplies its own version of area(), replacing the one it inherited from Shape. This is method overriding: same name, same parameters, different body. A circle of radius 2 answers about 12.57, a square of side 4 answers 16, a triangle with base 6 and height 4 answers 12.
- Step 4: Dynamic dispatch. The variable s is declared as a Shape, but the object it refers to is a Circle. When s.area() is called, the language looks at the actual object at run time and runs that class’s version. Choosing the method from the object, not from the variable’s declared type, is called dynamic dispatch.
- Step 5: The if-chain is gone. Put the three shapes in a list and ask each for its area. The loop contains no if and no mention of circles, squares or triangles: every call is dispatched to the right class by the object itself. The total comes to 12.57 + 16 + 12 = 40.57.
- Step 6: A new shape, no edits. Now add a rectangle. Write one new class with its own area(), and put a Rect of 3 by 5 in the list. The loop is not touched, and neither is any other code that works with Shapes. Compare step 1, where the same change meant editing every function that had a chain.
- Step 7: Overloading is different. Overloading looks similar but is a separate idea: several methods in one class share a name and differ in their parameters. The compiler picks one by looking at the arguments in the call, before the program runs. Overriding is settled at run time, by the object; overloading at compile time, by the arguments.
Try it yourself
Learn this first
Learn next
- Strategy patternSwappable rules behind one slot, replacing a chain of ifs with a function you pass in. Beginner
- Open/closed principleAdd a new case by writing new code, without reopening code that already works. Intermediate
- Dependency inversion and injectionHand an object the tools it needs instead of letting it build its own. Intermediate
Spotted a mistake on this page?