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 are the signs that code violates the Liskov Substitution Principle?
Subclasses that throw UnsupportedOperationException, override a method to do nothing, or callers that must check instanceof before using a parent-typed object.
IntermediateQuick answer1 min read - Why does Square extending Rectangle violate the Liskov Substitution Principle?
A Square must keep width and height equal, so setting one changes the other. Code written for any Rectangle, expecting independent sides, gets wrong results when given a Square.
IntermediateQuick 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