User Tools

Site Tools


principles:liskov_substitution_principle

Liskov Substitution Principle (LSP)

Variants and Alternative Names

Context

Principle Statement

“Subtypes must be substitutable for their base types.”1)

Description

Object-oriented programming languages allow to derive subtypes from base types and subtype polymorphism allows to pass an object of a subtype where ever an object of the supertype is specified. Suppose P and Q are types (i.e. classes or interfaces) and Q is derived from P (so Q is the subtype and P is the base type or supertype). A method m requiring a parameter of type P can be called with objects of type Q because every object of type Q is also an object of type P. This is always true as typically object-oriented programming languages are constructed in that way.

But the programming language does not enforce that the subtype also behaves like the supertype. Method m may work with an object of type P but not with an object of type Q. LSP demands that a subtype (Q in the example) has to be constructed in a way that it behaves like the supertype if it is called through the supertype interface. Q may have further methods and it may do additional things not observable by m but m shall be able to safely assume that its parameter behaves like an object of type P with respect to all observable state.

Rationale

Let P and Q be types and Q a subtype of P. If LSP is not adhered to, there is an operation accessible through the interface of P which behaves differently when called on Q. So code which is written in terms of P will not expect the behavior and will not work as desired.

Strategies

  • Only strengthen invariants in subclasses; never weaken them
  • Only weaken preconditions when overriding methods
  • Only strengthen postconditions when overriding methods
  • Use Delegation instead of Inheritance
  • Figure out better abstractions

Caveats

See section contrary principles.

Origin

Evidence

  • Examined LSP describes an effect created by object-oriented type systems. There is no human factor in there, so experiments are not needed. The effect was described and thoroughly examined by Barbara Liskov and Jeanette Wing2). Their reasoning is presented in section rationale in a simplified form.
  • Accepted LSP is widely known in practice, mainly because it is part of Robert C. Martin's SOLID principle collection.

Relations to Other Principles

Generalizations

Specializations

Contrary Principles

Complementary Principles

  • Model Principle (MP): MP demands inheritance relations to resemble an “is-a” relationship. This means that an object of the subclass is also an object of the superclass. This is always true in a technical sense as this is how object-oriented programming languages handle inheritance hierarchies. However MP demands that is shall be true in the model, too. This is slightly different from LSP which rather is about a “is-substitutable-for” relationship.
  • Principle of Separate Understandability (PSU): When building inheritance hierarchies, LSP constrains how subclasses are constructed. Namely they should comply with the superclass contract. PSU on the other hand demands that the superclass shall be separately understandable, which means that knowledge of concrete subclasses and their needs should not be necessary to understand the superclass. So a superclass should not have a specific functionality, etc. just because a particular subclass needs this. In contrast to that the superclass of course may provide protected features for subclasses in general. But it should be inherently clear that subclasses in general may need this functionality without looking at a particular one.

Principle Collections

OOD Principle Language
General Principles
ML KISS MIMC DRY GP RoE
Modularization Principles
MP HC ECV
Module Communication Principles
TdA/IE LC DIP
Interface Design Principles
EUHM PLS UP
Internal Module Design Principles
IH/E IAP LSP PSU

Examples

Description Status

Further Reading

Discussion

Discuss this wiki article and the principle on the corresponding talk page.

1)
Robert C. Martin: Agile Software Development, Principles, Patterns, and Practices, p. 111
principles/liskov_substitution_principle.txt · Last modified: 2016-03-07 22:17 by 66.228.70.98