Bird
Raised Fist0

Consider a subclass that overrides a method to accept a broader input domain than the superclass method. Under what conditions can this design still comply with LSP?

hard🚀 Application Q8 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
Consider a subclass that overrides a method to accept a broader input domain than the superclass method. Under what conditions can this design still comply with LSP?
AIf the subclass method weakens preconditions without violating expected behavior for superclass inputs.
BIf the subclass method strengthens preconditions to reject some superclass inputs.
CIf the subclass method changes return types to unrelated types.
DIf the subclass method introduces new side effects altering global state.
Step-by-Step Solution
Solution:
  1. Step 1: Recall LSP precondition rules

    LSP allows weakening preconditions (accepting more inputs) but forbids strengthening them.
  2. Step 2: Analyze options

    B correctly states that broadening input domain (weakening preconditions) is allowed if behavior remains consistent. A violates LSP by strengthening preconditions. C and D break substitutability by changing return types or side effects.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Weakening preconditions is LSP-compliant [OK]
Quick Trick: Weakening preconditions is allowed; strengthening is not [OK]
Common Mistakes:
MISTAKES
  • Thinking any input domain change breaks LSP
  • Confusing precondition strengthening with weakening
Trap Explanation:
PITFALL
  • Candidates often misinterpret input domain changes as always violating LSP.
Interviewer Note:
CONTEXT
  • Tests subtle understanding of precondition variance in LSP.
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