Interview Q&A / Object-Oriented Programming
Object-Oriented Programming interview questions
The four pillars, interfaces, abstract classes and design basics.
Prefer rapid revision? Practise Object-Oriented Programming short interview answers →
- What is the difference between compile-time and runtime polymorphism?
Compile-time (static) polymorphism is resolved by the compiler using the declared types, mainly through method overloading. Runtime (dynamic) polymorphism is resolved by the JVM using the actual object, through method overriding and dynamic dispatch. Fields, static methods and private methods are never resolved at runtime.
AdvancedIn-depth4 min read - Predict the output: a parent reference holds a child object that hides the parent's field. Which value do you get?
Methods are chosen by the object's real type, but fields are chosen by the reference type. So p.name gives the parent's value while p.get() runs the child's method.
AdvancedQuick answer1 min read - What is a covariant return type in method overriding?
An overriding method may return a more specific type (a subclass) than the parent's method declared. Callers using the child type then avoid casting.
AdvancedQuick answer1 min read - Why do APIs use static factory methods like List.of() instead of constructors?
Static factory methods have names, can return cached or existing objects, can return a subtype, and can hide the implementation class. Constructors can do none of these.
AdvancedQuick answer1 min read