Bird
Raised Fist0

In which scenario is applying the Liskov Substitution Principle (LSP) most critical to ensure system correctness?

easy🔍 Pattern Recognition Q11 of Q15
OOP & Design Patterns - Liskov Substitution Principle - Subtype Behavioural Contract
In which scenario is applying the Liskov Substitution Principle (LSP) most critical to ensure system correctness?
AWhen a subclass adds new methods without overriding any superclass methods.
BWhen a subclass uses composition instead of inheritance.
CWhen a subclass narrows the input parameter types of an overridden method.
DWhen a subclass extends a superclass but changes the expected behavior of inherited methods.
Step-by-Step Solution
  1. Step 1: Understand LSP's core requirement

    LSP requires that subclasses can replace their superclasses without altering desirable properties of the program, especially behavior.
  2. Step 2: Analyze each option carefully

    When a subclass extends a superclass but changes the expected behavior of inherited methods. describes a subclass changing expected behavior, which violates LSP. When a subclass adds new methods without overriding any superclass methods. is safe as adding methods doesn't break substitutability. When a subclass narrows the input parameter types of an overridden method. narrows input types (contravariance violation), which breaks substitutability but is less direct than changing behavior. When a subclass uses composition instead of inheritance. is unrelated to LSP since composition is an alternative to inheritance.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Only changing inherited method behavior breaks LSP directly.
Quick Trick: LSP is about preserving inherited behavior, not just adding features.
Common Mistakes:
MISTAKES
  • Thinking adding methods breaks LSP
  • Confusing covariance and contravariance in parameters
  • Assuming composition relates directly to LSP
Trap Explanation:
PITFALL
  • Option B clearly violates LSP by changing expected behavior; option C is a subtler violation but less critical here.
Interviewer Note:
CONTEXT
  • Tests candidate's ability to identify when LSP applies and what constitutes a violation.
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