Subtype must be able to replace their base type
Suppose there is a function f, its parameter is a pointer or reference pointing to a base class B, and also assumes a derived class D in B, if the object of D is used as
B-type passed to F, which causes F to have an error behavior, then D will violate the LSP Martin for an example: public class rectangle () {private int width; private int Height; public void setwidth (int W) {this. Width = W;} public void setHeight (int h) {this.height = h;} public int getarea () {return this.height * this.width;}}
Natural Square is inherited from Rectangle, but two methods are overwritten, ensure long and wide are consistent public class square extend rectangle () {public void setwidth (int W) {Super.SetWidth (W); Super. SetHeight (w);} public void setheight (int h) {
Super.SetWidth (H); Super.SetHeight (H);}}
It is assumed to have the following function g (Rectangle R) {R.SetWidth (5); R.SetHeight (4); assert (r.Getarea () == 20);} When the object being passed is Square, it will An error !!! This is a violation of the LSP DBC-based design: pre-condition, post-conditionation, invariant (see MEYER's book "based on DBC design")
The LSP makes OCP possible, the replaceability of the subtype to the parent type must be something that developers can implicitly dependent, so if there is no explicit force base type, those code must be good and obviously expressed. at this point.