Polymorphism
OOP · 10 interview questions
Polymorphism means the same call can do different things depending on the actual type behind it. Write code against Shape, call area(), and get the circle's implementation or the square's without the calling code knowing or caring which it holds.
It is usually split into two kinds. Runtime polymorphism comes from overriding: the method is chosen by the object's actual type while the program runs, via a virtual dispatch table. Compile-time polymorphism comes from overloading: several methods share a name and the compiler picks one from the argument types.
The distinction matters because only the runtime kind gives you the real benefit — code that works with types that did not exist when it was written. Overloading is a convenience for the person writing the call.
Polymorphism interview questions
- What is polymorphism?
- The ability of a single interface or call site to work with objects of different types, with the actual behaviour determined by the object rather than the caller.
- What's the difference between compile-time and runtime polymorphism?
- Compile-time polymorphism is overloading — the compiler picks the method from the argument types. Runtime polymorphism is overriding — the method is chosen from the object's actual type as the program runs.
- Overloading versus overriding — what actually differs?
- Overloading means same name, different parameter list, within one class; resolved by the compiler. Overriding means same name and same signature, in a subclass, replacing the parent's implementation; resolved at run time.
- Why they ask: Asked constantly, and easy to blur under pressure. Say 'same signature' for overriding — that's the discriminator.
- How does runtime dispatch actually work?
- Most implementations give each class a table of function pointers — a vtable — and each object a hidden pointer to its class's table. A virtual call reads the pointer and jumps through the table, so the cost is one extra indirection.
- Are methods virtual by default?
- It depends on the language. In Java, instance methods are virtual unless marked final or static. In C++ and C#, methods are non-virtual by default and must be declared virtual explicitly. Getting this backwards is a common slip.
- Can static methods be overridden?
- No. Statics belong to the class, not an instance, so there is no object to dispatch on. Declaring a static method with the same signature in a subclass hides the parent's rather than overriding it, and the version chosen depends on the reference type.
- Why they ask: A classic trick question — the code compiles, so people assume it overrides.
- Is overload resolution based on the declared type or the runtime type of the argument?
- The declared type. Overloads are chosen by the compiler from static types, so passing an Object reference holding a String picks the Object overload, not the String one.
- What is parametric polymorphism?
- Writing code that works over a type parameter rather than a concrete type — generics in Java or C#, templates in C++. A List<T> works for any T without duplicating the implementation per type.
- What is duck typing?
- Deciding whether an object is acceptable by whether it has the methods you need, not by its declared type. Common in Python and Ruby: if it responds to read(), it can be used as a file-like object regardless of its class.
- What concrete benefit does runtime polymorphism give you?
- You can add new types without changing existing code. A render loop over Shape works for a Triangle written a year later, because the loop depends on the interface rather than the list of implementations.
You'll forget most of this by next week
That's not a discipline problem, it's how memory works. In the app these come back on an expanding schedule — right before you'd lose them.
Start free for 7 days