Summary
Functions ' use pointers or references to base classesmust is able to-use objects of derived classes-without knowing it .
All references to base classes must be able to transparently use objects of their subclasses.
That
◇ so use the base class code place, with the derivation class code replaces, can perform the action processing correctly.
In other words, if the derived class replaces the base class and does not perform the action correctly, then their inheritance should be abolished.
In other words, only OO designs that meet the following 2 conditions can be considered to satisfy the LSP principle:
◇ you should not have a condition in your code to judge a subclass type such as If/else. The following code violates the LSP definition.
View plain if (obj typeof Class1) {do something} else if (obj typeof Class2) {do something else}
◇ subclasses should be able to replace the parent class and appear anywhere the parent class can appear.
Or if we replace the base class in the code with its subclasses, the code will work properly.
The Richter replacement principle LSP is an important guarantee for the code to conform to the open and closed principles. At the same time LSP embodies:
◇ Class Inheritance principle: If an object of an inheriting class might run wrong where the base class appears,
The subclass should not inherit from the base class, or it should redesign the relationship between them.
◇ Action Correctness Guarantee: From the other side, the extension of the class that accords with the LSP design principle will not introduce new errors to the existing system.
the inheritance principle of the class:
Robert C. Martin, in introducing Liskov substitution principle (LSP), gave an example of rectangle and square.
This example is used here, but it is rewritten in the Java language and ignores some details and lists only the following essentials
Section to illustrate the rule of the Richter substitution principle on inheritance of classes.
View plain//code: class rectangle { double width; doubleheight; public double getheight () { returnheight; } public void setheight (doubleheight) { this