Skip to main content

Why is LSP related to inheritance?

LSP is related to inheritance because this principle defines how inheritance must work for polymorphism to remain correct and safe.

In detail:

1. Inheritance implies substituting the child class for the parent

The main goal of inheritance is to make it possible to use a descendant object wherever an object of the parent class is expected. Polymorphism only works when this kind of behavior is safe.

2. LSP defines the rules of correct polymorphism

A descendant is obligated to preserve the meaning and contract of the parent. If a descendant changes behavior so that client code stops working correctly, inheritance is being used incorrectly.

3. Inheritance without LSP breaks the type system

If a subclass cannot be substituted for the base class, inheritance becomes a source of bugs:

  • the client has to check types (if obj is Child),
  • OCP gets violated,
  • abstractions stop working,
  • the code becomes fragile. LSP prevents such situations.

4. Inheritance creates a contract the subclass is obligated to follow

The contract includes:

  • allowed input parameters,
  • guaranteed results,
  • state invariants,
  • allowed exceptions. LSP says: a descendant has no right to violate this contract.

5. Without LSP, inheritance loses its purpose as an extension mechanism

If a descendant behaves differently than the client expects, inheritance no longer provides an advantage - it becomes a source of regressions.

6. LSP is the foundation for applying inheritance in SOLID

All the SOLID principles rely on inheritance working correctly. If LSP is violated, the other principles (SRP, OCP, ISP, DIP) also start to break, because abstractions stop being reliable.

Summary: LSP is related to inheritance because it is exactly what determines when inheritance is correct: a subclass must fully preserve the parent's contract so that it can be substituted into any code that uses the base type, without changing the program's behavior.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.