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?
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:
Step 1: Recall LSP precondition rules
LSP allows weakening preconditions (accepting more inputs) but forbids strengthening them.
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.
Final Answer:
Option A -> Option A
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.