Liskov substitution principle
Encyclopedia : L : LI : LIS : Liskov substitution principle
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 [q(x)] be a property provable about objects [x] of type [T]. Then [q(y)] should be true for objects [y] of type [S] where [S] is a subtype of [T].
A function using a class hierarchy violating the principle uses a reference to a base class but must know about all the derivatives of that base class. Such a function violates the open/closed principle because it must be modified whenever a new derivative of the base class is created.
In the design by contract methodology, the principle leads to some restrictions on how contracts can interact with inheritance:
- Preconditions must be the same or weaker in a subclass. This means that you cannot have a subclass that has stronger preconditions than its superclass.
- Postconditions must be the same or stronger in a subclass. This means that you cannot have a subclass that has weaker postconditions than its superclass.
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
- Citeseer page for the original paper: http://citeseer.ist.psu.edu/liskov94family.html
From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.
