Liskov substitution principle

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by ThomasOwens (talk | contribs) at 13:11, 13 August 2008 (Propsed merge with Inheritance (computer science).). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In object-oriented programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov in a 1987 conference keynote address entitled Data abstraction and hierarchy [1].

Liskov formulated the principle succinctly in a subsequent paper[2] as follows:

Let be a property provable about objects of type . Then should be true for objects of type where is a subtype of .

Thus, Liskov's notion of "subtype" is based on the notion of substitutability; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness).

Design by contract

The Liskov substitution principle is closely related to the design by contract methodology, leading to some restrictions on how contracts can interact with inheritance:

In addition, the principle implies that no new exceptions should be thrown by methods of the subclass, except where those exceptions are themselves subtypes of exceptions thrown by the methods of the superclass. See covariance and contravariance.

A function using a class hierarchy violating the principle uses a reference to a base class, yet must have knowledge of the subclasses. Such a function violates the open/closed principle because it must be modified whenever a new derivative of the base class is created.

References

  1. ^ Liskov, Barbara (1987-10-04). "Data abstraction and hierarchy". Retrieved 2008-03-23. {{cite web}}: Check date values in: |date= (help)
  2. ^ Liskov, Barbara (July 1999). "Behavioral Subtyping Using Invariants and Constraints" (PS). Retrieved 2006-10-05. {{cite web}}: Cite has empty unknown parameter: |1= (help); Unknown parameter |coauthors= ignored (|author= suggested) (help)