Imagine a subclass overrides a method to throw an unchecked exception in some cases where the superclass method never does. What is the likely effect when a client uses the subclass instance polymorphically?
AThe client will handle the exception gracefully since unchecked exceptions are always safe to add.
CThe subclass method's exception will be ignored due to polymorphism, so no effect occurs.
DThe superclass method's contract automatically adapts to include the new exception.
Step-by-Step Solution
Solution:
Step 1: Recall LSP and exception behavior
LSP requires that subclass methods do not introduce new exceptions that clients are unprepared to handle.
Step 2: Analyze the scenario
Adding unchecked exceptions in subclass methods can cause unexpected runtime failures when clients expect no exceptions, breaking substitutability.
Step 3: Evaluate options
D correctly states the client may experience unexpected runtime failures. A is false because unchecked exceptions can break client assumptions. B is false; superclass contracts do not adapt automatically. C is incorrect; exceptions are not ignored.
Final Answer:
Option B -> Option B
Quick Check:
Unexpected exceptions break client assumptions and LSP [OK]
Quick Trick:New exceptions in subclass methods break LSP substitutability [OK]
Common Mistakes:
MISTAKES
Assuming unchecked exceptions are always safe to add
Believing polymorphism hides exceptions
Trap Explanation:
PITFALL
Candidates underestimate the impact of new exceptions on client code relying on superclass contracts.
Interviewer Note:
CONTEXT
Assesses understanding of exception handling in behavioral contracts under LSP.