Bird
Raised Fist0

Which design scenario best exemplifies the need to apply the Liskov Substitution Principle (LSP) to avoid runtime errors?

easy🔍 Pattern Recognition Q1 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
Which design scenario best exemplifies the need to apply the Liskov Substitution Principle (LSP) to avoid runtime errors?
AReplacing a superclass instance with a subclass that narrows the method's input domain causing unexpected exceptions.
BUsing inheritance solely to reuse code without overriding any methods.
CAdding new methods to a subclass without changing existing superclass methods.
DImplementing interfaces without any inheritance hierarchy.
Step-by-Step Solution
Solution:
  1. Step 1: Identify the core of LSP

    LSP requires that subclasses can replace superclass instances without altering desirable properties, including correctness.
  2. Step 2: Analyze each option

    C describes a subclass that restricts input domain, violating substitutability and causing runtime errors. A, B, and D do not inherently violate LSP as they don't break behavioral contracts.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Subclass narrowing input domain breaks substitutability [OK]
Quick Trick: LSP fails when subclass restricts superclass method inputs [OK]
Common Mistakes:
MISTAKES
  • Confusing code reuse with behavioral substitutability
  • Ignoring input domain restrictions as LSP violations
Trap Explanation:
PITFALL
  • Candidates often think any inheritance reuse is LSP-compliant, missing behavioral contract violations.
Interviewer Note:
CONTEXT
  • Reveals understanding of behavioral contracts and substitutability in inheritance.
Master "Liskov Substitution Principle - Subtype Behavioural Contract" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes