Bird
Raised Fist0

What is the expected impact when the subclass instance replaces the superclass instance in client code?

medium🧠🧾 Concept Trace Q4 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
A subclass overrides a method and changes its side effects by modifying global state, whereas the superclass method does not. What is the expected impact when the subclass instance replaces the superclass instance in client code?
ANo impact, since side effects are irrelevant to LSP compliance.
BThe subclass method's side effects are automatically encapsulated, so clients are unaffected.
CImproved performance due to side effect optimizations in the subclass.
DPotentially incorrect client behavior due to unexpected side effects violating LSP.
Step-by-Step Solution
Solution:
  1. Step 1: Understand LSP and side effects

    LSP requires behavioral substitutability, including preserving observable side effects expected by clients.
  2. Step 2: Analyze the scenario

    Changing side effects in subclass methods can cause clients to observe unexpected state changes, breaking substitutability.
  3. Step 3: Evaluate options

    D correctly identifies potential incorrect client behavior. A is false; side effects matter. B is false; side effects are not automatically encapsulated. C is unrelated to LSP.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Unexpected side effects break behavioral substitutability [OK]
Quick Trick: Side effects changes can break LSP substitutability [OK]
Common Mistakes:
MISTAKES
  • Ignoring side effects as part of behavioral contracts
  • Assuming encapsulation hides side effects automatically
Trap Explanation:
PITFALL
  • Candidates often overlook side effects when reasoning about LSP compliance.
Interviewer Note:
CONTEXT
  • Tests awareness of behavioral contracts beyond method signatures.
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