Bird
Raised Fist0

Trace the sequence of events when a client calls a method on a subclass instance that violates the Liskov Substitution Principle by strengthening a postcondition. What happens step-by-step?

easy🧠🧾 Concept Trace Q12 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
Trace the sequence of events when a client calls a method on a subclass instance that violates the Liskov Substitution Principle by strengthening a postcondition. What happens step-by-step?
AThe client receives a result that meets the superclass contract, so no issues arise.
BThe subclass method returns a stricter result than expected, potentially causing client failures.
CThe client silently ignores the stricter postcondition, so behavior is unaffected.
DThe subclass method throws an exception due to the strengthened postcondition.
Step-by-Step Solution
  1. Step 1: Recall LSP postcondition rule

    Subclasses must not strengthen postconditions; they can only maintain or weaken them.
  2. Step 2: Trace client call

    The client expects results conforming to the superclass contract. If subclass returns stricter results, some clients expecting broader results may fail.
  3. Step 3: Analyze options

    The subclass method returns a stricter result than expected, potentially causing client failures. correctly identifies potential client failures due to stricter postconditions. The client receives a result that meets the superclass contract, so no issues arise. is false because stricter postconditions can break clients. The client silently ignores the stricter postcondition, so behavior is unaffected. is incorrect; clients cannot ignore contract violations silently. The subclass method throws an exception due to the strengthened postcondition. is not guaranteed; exceptions are not implied by postcondition strengthening.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Strengthening postconditions risks breaking client expectations.
Quick Trick: Strengthening postconditions breaks client assumptions and causes failures.
Common Mistakes:
MISTAKES
  • Assuming stricter postconditions are safe
  • Believing clients ignore contract violations
  • Confusing exceptions with contract violations
Trap Explanation:
PITFALL
  • Options A and C reflect common misconceptions that stricter guarantees are harmless or ignored; option D confuses contract violation with runtime exceptions.
Interviewer Note:
CONTEXT
  • Tests understanding of behavioral contracts and consequences of violating postconditions.
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