Top 4 Inheritance Interview Questions
Browse 4 real-world technical and behavioral interview questions about Inheritance. Review scenarios, edge cases, and architectural best practices.
Give me a subclass that the compiler accepts but that still breaks the Liskov substitution principle. What rule does it break?
Substitutability is a claim about behaviour, and signatures carry none of it. A subclass that rejects an argument its supertype accepted has strengthened a precondition, which the compiler cannot see: the code type-checks, and it breaks callers that were written correctly against the base type.
Where does encapsulation or polymorphism change a design, and how do you decide between composition and inheritance?
Encapsulation earns its place when a class owns an invariant that callers would otherwise each have to enforce, and polymorphism when a conditional grows with every new requirement. Composition versus inheritance is decided on evidence: how many axes vary, and whether every inherited operation is honest.
What are the four pillars of OOP, and what is the difference between abstraction and encapsulation?
Encapsulation, inheritance, polymorphism and abstraction are worth stating as consequences rather than definitions - what each one buys you and what it costs. Abstraction decides what a type means to its caller; encapsulation protects how that meaning is upheld.
When would you use the strategy pattern instead of inheritance?
Use the strategy pattern instead of inheritance when behaviour varies independently from object identity. Composition avoids subclass explosion, enables runtime swapping and makes algorithms easier to test.