Liskov's principle of substitution

from Wikipedia, the free encyclopedia

The Liskov substitution principle ( LSP ) or Ersetzbarkeitsprinzip is a criterion in the object-oriented programming , that the conditions for modeling a data type for its subtype indicates. It states that a program that uses objects of a base class T must also function correctly with objects of the class S derived from it, without changing the program.

The Liskov substitution principle was first presented in 1987 by Barbara Liskov at a conference Data abstraction and hierarchy and was formulated in 1993 by Barbara Liskov and Jeannette Wing . In a subsequent article it was formulated as follows (translation):

"A stronger requirement [than covariance and contravariance ] is needed that restricts the behavior of subtypes: Properties that can be proven using the specification of the supposed type of an object should also apply if the object belongs to a subtype of this type:

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

This guarantees that operations which are applied to an object of the type of the type are also carried out correctly. In some of the programming languages ​​used today that support polymorphism , this principle can be violated by inheritance from more than one object to another. Then it would not always be possible to replace an object of the type with an object of the type without hesitation .

In relation to individual methods , Liskov's substitution principle means that when a method is overwritten by a derived class, the preconditions may only be weakened and the postconditions may only be strengthened (see Design by Contract ).

The problem

UML representation of the GrafischesElement class and its subclasses

Inheritance is an important element of object-oriented programming : a class (the subclass) is derived from another class (its superclass) and inherits its methods and data elements. New data elements can be added and methods can be added or replaced.

This leads to the question of what inheritance says about the relationship of the superclass to the subclass. This question is usually answered with: Inheritance describes an is-a relationship. A typical hierarchy of classes in a graphics program could e.g. B. consist of a superclass GrafischesElementand subclasses derived from it such as Rechteck, Ellipseor Text. For example, one will justify the derivation of the class Ellipsefrom the class GrafischesElementwith: An ellipse is a graphic element. The class GrafischesElementcan then zeichnedefine, for example, a general method Ellipsethat is replaced by a method that specifically draws an ellipse.

The problem here, however, is that the “ is-a criterion” is sometimes misleading. For example, if the class is Kreisdefined for the graphics program , one would derive this class from naively applying the “ is-a criterion” Ellipse, because a circle is an ellipse, namely an ellipse with semiaxes of equal length. However, this derivation can be incorrect in the context of the graphics program: graphics programs usually allow the graphic elements to be changed. For example, in the case of ellipses, the length of the two semi-axes can be changed independently of one another. However, this does not apply to a circle, because after such a change it would no longer be a circle. If the class has Ellipsethe methods SkaliereXand SkaliereY, the class would Kreisinherit these methods even though their use is not permitted for a circle.

Liskov's principle of substitution reveals the problem here. In the present case, it would be established that the statement “the axes can be scaled independently of one another” applies to the class Ellipse, but not to the class Kreis. If, however, were Kreisa subclass of Ellipse, then according to Liskov's principle of substitution, this statement should also apply to the class Kreis. Hence there is Kreisno subclass of here Ellipse.

It should be noted here that the decision depends on the specific case. If, for example, a manipulation of the geometric figure after creation is not intended, it can Kreisbe Ellipsederived from: Then "the axes can be scaled independently of each other" is not a property of the class Ellipse, and therefore it does not have to be a property of in Kreisorder Kreisto be a subclass of Ellipseclose.

Individual evidence

  1. Barbara H. Liskov, Jeannette M. Wing: Family Values: A Behavioral Notion of Subtyping . Pittsburgh 1993. ( PostScript )
  2. Barbara H. Liskov, Jeannette M. Wing: Behavioral Subtyping Using Invariants and Constraints . Pittsburgh 1999 ( PostScript ).