Loading…
Loading…
Charting your lesson…
Opening your lesson…
Polymorphism: Compile-time vs Runtime
Lesson 4 of 4
6 steps•about 12 min•12 flashcards
Step 1/ 61 min
Polymorphism — "many forms" — is what lets one interface drive many behaviours. Distinguishing its two flavours (compile-time vs runtime) and knowing the mechanism behind each is a guaranteed interview topic.
Core idea: polymorphism lets the same call take different forms depending on context. Compile-time (static) polymorphism is resolved by the compiler — method overloading and generics. Runtime (dynamic) polymorphism is resolved during execution by the object's actual type — method overriding, dispatched through a virtual table.
Step 2/ 61 min
// Compile-time: overloading (same name, different params) -> compiler picks
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
// Runtime: overriding (subclass redefines) -> object's real type picks
class Animal { void sound() { print("..."); } }
class Dog extends Animal { void sound() { print("woof"); } }
The heart of runtime polymorphism:
Animal a = new Dog(); // reference type Animal, actual object Dog
a.sound(); // prints "woof" -- the DOG's version runs
Even though a is declared Animal, the call dispatches to Dog.sound() because the JVM looks up the method on the actual object, not the reference type. This "upcast the reference, dispatch on the object" pattern is how you write code against a base type that transparently runs specialised behaviour.
Runtime dispatch is implemented with a virtual method table (vtable): each class has a table of function pointers to its method implementations, and each object carries a hidden pointer to its class's vtable. A virtual call is "look up slot k in the object's vtable and jump there" — one extra indirection. In C++ methods are dispatched statically unless marked virtual; in Java instance methods are virtual by default (use final/static to make them non-virtual).
Animal* a = new Dog();
a->sound(); // C++: virtual? -> Dog::sound via vtable; non-virtual -> Animal::sound
Step 3/ 61 min
Animal[] zoo = { new Dog(), new Cat(), new Cow() };
for (Animal x : zoo)
x.sound(); // woof / meow / moo -- each dispatches to its own type
One loop, three behaviours, and no if/instanceof chain. Adding a new Sheep subclass requires zero changes to this loop — it just works. That extensibility (the Open/Closed Principle in action) is the entire point of runtime polymorphism.
Step 4/ 61 min
Core asks: "compile-time vs runtime polymorphism" (overloading vs overriding), "how does dynamic dispatch / vtable work", "output of an upcast call" (Animal a = new Dog(); a.sound()), and in C++ "what does virtual do / why do you need a virtual destructor?" A missing virtual destructor in C++ leaks when deleting a derived object via a base pointer.
Step 5/ 61 min
virtual means a base-pointer call runs the base version (static binding).Expecting field access or static methods to be polymorphic. Only overridden instance methods are dispatched on the object's real type; fields and static methods bind to the reference (declared) type. So Animal a = new Dog(); a.legs reads Animal.legs, not Dog.legs — a subtle but frequently tested distinction.
Animal a = new Dog()).Step 6/ 6· Worked examples2 min
Problem
Predict the output and explain which calls are resolved at compile time and which at runtime.
class Animal {
String speak() { return "..."; }
static String kind() { return "Animal"; } // static
}
class Dog extends Animal {
String speak() { return "Woof"; } // override (runtime)
static String kind() { return "Dog"; } // hides (compile-time)
String speak(int times) { return "Woof x" + times; } // overload
}
public class Demo {
public static void main(String[] a) {
Animal x = new Dog(); // static type Animal, dynamic type Dog
System.out.println(x.speak()); // (A)
System.out.println(x.kind()); // (B)
Dog d = new Dog();
System.out.println(d.speak(3));// (C)
}
}
Try it on paper first.
Recall
Keep this
Polymorphism — "many forms" — is the fourth OOP pillar and the concept interviewers probe most for depth. The compile-time vs runtime split, and how runtime dispatch actually works (virtual tables), separate strong candidates.
| Compile-time | Runtime | |
|---|---|---|
| Mechanism | overloading | overriding |
| Binding | early (compiler) | late (runtime) |
| Needs | — | inheritance + virtual/override |
The base method must be virtual (C++) / non-final (Java, virtual by default). The compiler builds a vtable (virtual method table) per class; each object holds a pointer to it, so a call is dispatched to the correct override based on the actual object type, not the reference type.
virtual, else you get static binding.final/static/private methods are not overridable (statically bound).static or non-virtual method call to dispatch dynamically — it binds by the reference/declared type."Compile-time vs runtime polymorphism", "how does dynamic dispatch/vtable work?", "overloading vs overriding", "what does virtual do?", and predict-the-output with base references to derived objects.
Polymorphism = many forms. Compile-time (overloading, early binding) vs runtime (overriding, late binding). Runtime needs inheritance + virtual/overridable methods; dispatch via per-class vtable using the object's actual type. Java methods are virtual by default; static/final/private are statically bound.
You've read 0 of 6 steps
Now lock it in — questions straight after reading are what make it stick.