Liskov substitution principle
From CSSEMediaWiki
Revision as of 02:25, 24 September 2008 by David Thomson (Talk | contribs)
The Liskov substitution principle (LSP) is a particular definition of subtype that was introduced by Barbara Liskov, in 1987. Succinctly, it states
- 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.
In English, this means that if S is a subclass of T, then any objects of type T in a program can be replaced by object of type S, without changing the correctness of the system. Basically, subclasses should behave exactly like super classes, except with a little extra functionality, but no less.
This is closely related to Design by contract, which states that
- Preconditions cannot be strengthened in a subclass.
- Postconditions cannot be weakened in a subclass.
A violation of this principle will occur when a class uses a reference to a base class, but has some knowledge of the subclass(s).