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 method overloading and method overriding?
Overloading means several methods in the same class share a name but take different parameters, and the compiler picks one. Overriding means a subclass gives its own version of a method it inherited, with the same signature, and the JVM picks the version at runtime based on the actual object.
BeginnerIn-depth3 min read - Can static methods be overridden in Java?
No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.
IntermediateQuick answer1 min read - What is dynamic method dispatch in Java?
It is the way Java chooses which overridden method to run at runtime, based on the actual object type and not on the type of the reference variable.
IntermediateQuick 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 - What rules must an overriding method follow to respect the Liskov Substitution Principle?
It must accept at least what the parent accepts, promise at least what the parent promises, and throw no new kinds of failure. Weaker preconditions, stronger postconditions, same invariants.
AdvancedQuick answer1 min read