Liskov substitution principle

From Free net encyclopedia

Revision as of 13:42, 4 April 2006; view current revision
←Older revision | Newer revision→

In object-oriented programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov and Jeannette Wing in a 1993 paper entitled Family Values: A Behavioral Notion of Subtyping. (It is not the only definition; see datatype.)

The principle was formulated succinctly in a subsequent paper as follows:

Let <math>q(x)</math> be a property provable about objects <math>x</math> of type <math>T</math>. Then <math>q(y)</math> should be true for objects <math>y</math> of type <math>S</math> where <math>S</math> is a subtype of <math>T</math>.

Thus, Liskov and Wing'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).

In the design by contract methodology, the principle leads to some restrictions on how contracts can interact with inheritance:

  • Preconditions are weakened in a subclass. This means that you cannot have a subclass that has stronger preconditions than its superclass.
  • Postconditions are strengthened in a subclass. This means that you cannot have a superclass that has stronger postconditions than its subclass.

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.

External links

it:Principio di sostituzione di Liskov