Liskov substitution principle

The Liskov Substitution Principle (LSP ) or Ersetzbarkeitsprinzip is a criterion in the object-oriented programming, which specifies the conditions for the modeling of a data type for its subtype. It states that a program that objects of a base class used T, must work correctly even with objects of the class derived from S, without changing the program.

The Liskov Substitution Principle was formulated by Barbara Liskov and Jeannette Wing 1993. In a subsequent article, it was formulated as follows ( translation ):

" A stronger requirement [ as covariance and contravariance ] is used which restricts the behavior of subtypes: properties that can be demonstrated by the specification of the assumed type of an object, should also be considered if the object belongs to a subtype of this type:

This guarantees that operations that are applied to an object of type type, are also carried out correctly. In some of today programming languages ​​that support polymorphism, this principle can be violated by inheritance from more than one object to another. Then, an object of type could not always safe to replace an object of type.

The problem

An important element of object-oriented programming is inheritance: A class ( the subclass ) is from another class ( its superclass ) is derived and it inherits their methods and data elements. This new data elements can be added as well as methods added or replaced.

This leads to the question of what inheritance says about the relationship of the upper class to the lower class. This question is usually answered with: Inheritance describes an is -a relationship. A typical hierarchy of classes in a graphics program could, for example consist of a top class graphic element and derived subclasses such as rectangle, ellipse, or text. For example, the derivative of the class ellipse of the graphic element with class justify: An ellipse is a graphical element. The class Graphical element can then define the circle is replaced by, for example, to draw a general method. Using a method that specifically draws a circle

The problem here, however, that the " is - a - criterion " sometimes leads astray. For example, the class circle defined for the graphics program, as one would in a naive application of the " is - a - criterion" this class of ellipse to infer, as a circle is an ellipse, namely an ellipse with semi-axes of equal length. However, this derivation can be wrong in the context of the graphics program: graphics programs usually allow to change the graphical elements. For example, can be in ellipses, the length of the two semi-axes change independently. However, this does not apply for a circle, because after such a change, he would no longer be a circle. Thus, once the class ellipse methods SkaliereX SkaliereY and so would the class circle inherit these methods, although its application is not allowed for a circle.

The Liskov Substitution Principle reveals the problem here. In the present case would be found that the statement " the axes can be independently scaled " Although for the class ellipse, but not for the circle class applies. However, would circle a subclass of Ellipse, so would after liskovschen substitution principle, this statement also apply to the circle class. Hence circuit is here not a subclass of the ellipse.

It must be noted that the decision to use depends on the specific case. For example, a manipulation of the geometric figure after the creation is not provided, so you can loop well be derived from ellipse: Then " the axes can be independently scaled " not a property of the class ellipse, and thus they must also not a property of circuit be to make loop for subclass of ellipse.

314546
de