Bird
Raised Fist0

What is the likely effect when a client uses the subclass instance polymorphically?

easy🧠🧾 Concept Trace Q3 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
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.
BThe client may experience unexpected runtime failures violating LSP substitutability assumptions.
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:
  1. Step 1: Recall LSP and exception behavior

    LSP requires that subclass methods do not introduce new exceptions that clients are unprepared to handle.
  2. Step 2: Analyze the scenario

    Adding unchecked exceptions in subclass methods can cause unexpected runtime failures when clients expect no exceptions, breaking substitutability.
  3. 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.
  4. Final Answer:

    Option B -> Option B
  5. 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.
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