Loading…
Loading…
Charting your lesson…
Opening your lesson…
Polymorphism
Lesson 4 of 5
One interface, many forms — the same call dispatches to different implementations at runtime (or compile time).
6 steps•about 10 min•8 flashcards
Step 1/ 61 min
"Many shapes, one draw() call." Polymorphism is the OOP pillar that makes code extensible without endless if/else chains, and interviewers love it because it reveals whether you understand dynamic dispatch — not just memorized definitions.
Step 2/ 61 min
Polymorphism ("many forms") lets a single interface represent different underlying types, so the same call does the right thing depending on the actual object. You write code against a general type (Shape) and call a method (area()); at runtime the specific implementation (Circle's or Rectangle's) runs. This lets you add new types without changing the calling code.
With overriding, the method executed depends on the object's actual (runtime) type, not the reference's declared type. Under the hood this uses a virtual method table (vtable): each object knows which concrete method to jump to. This is why a List of Shape can hold Circles and Squares and call area() uniformly — each dispatches to its own implementation.
Polymorphism is what makes the Open/Closed Principle practical: to support a new Shape you add a class, not edit a switch statement. It also enables dependency injection and strategy patterns. Contrast with overloading (compile-time, same class, resolved by argument types) and note it's distinct from overriding (runtime, across the hierarchy). Beware: overusing type checks (instanceof / switch on type) is usually a sign you should be using polymorphism instead.
Step 3/ 61 min
A drawing app keeps a list of Shape objects. Iterating and calling shape.area() on each, a Circle returns pirr and a Rectangle returns w*h — the correct method is chosen per object at runtime. Adding a Triangle later just means a new class implementing area(); the rendering loop is untouched. Compare with overloading: a single class might define add(int,int) and add(double,double) — the compiler picks by argument types.
Step 4/ 61 min
Use runtime polymorphism whenever you have a family of types that share an interface but behave differently, and you want to add new types without touching existing code — replacing type-based conditionals with method dispatch. Use overloading for convenience variants of an operation on different argument types.
Step 5/ 61 min
Remember "one interface, many implementations — the object decides." Overriding = runtime (across classes); overloading = compile-time (same class, different signatures).
Don't confuse overriding with overloading. Overriding is runtime polymorphism (a subclass redefines a parent method, same signature); overloading is compile-time (same method name, different parameter lists in the same class). They are commonly mixed up in interviews.
Step 6/ 6· Worked examples1 min
Problem
Using the Animal/Dog/Cat classes (each overriding sound()), what does this print, and what OOP mechanism is at work?
Animal[] zoo = { new Dog(), new Cat(), new Animal() };
for (Animal a : zoo)
print(a.sound());
(Dog.sound() = "Woof", Cat.sound() = "Meow", Animal.sound() = "...")
Try it on paper first.
Recall
Keep this
Fast revision of polymorphism.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.