Definition 1: If the object O1 for each type is T1, there is an object O2 of type T2, so that all program P defined by T1 is substituted for O1 for all object O2, the behavior of the program P is not changed, then the type T2 is the subtype of the type T1.
definition 2: All references to base classes must be able to transparently use objects of their subclasses.
The origin of the problem: there is a functional P1, completed by Class A. Now need to expand the function P1, the function of the expansion of P, where p by the original function P1 and the new function P2 composition. The new function p is completed by subclass B of Class A, then the subclass B may cause the original function P1 to fail while completing the new function P2.
Solution: Follow the Richter substitution principle when using inheritance. When Class B inherits Class A, try not to rewrite the method of parent Class A, as well as try not to overload the method of the parent class A, in addition to adding new methods to complete the added P2.
Inheritance contains a layer of meaning: in a parent class, a method that is already implemented (as opposed to an abstract method), is actually setting a series of specifications and contracts, although it does not enforce that all subclasses must comply with these contracts, but if subclasses modify these non-abstract methods arbitrarily, they can cause damage to the entire inheritance system. The principle of the Richter substitution is to express the meaning of this layer.
Inheritance, as one of the three characteristics of object-oriented, brings about great convenience to program design, and also brings drawbacks. The use of inheritance, for example, is invasive to the program, the portability of programs increases the coupling between objects, and if a class is inherited by other classes, then when the class needs to be modified, all subclasses must be taken into account, and when the parent class is modified, all functions involving subclasses are likely to fail.
Example:
public class Rectangle {
int width;
int height;
Public Rectangle (int w, int h) {
width = w;
Height = h;
}
public int Getarea () {return
width*height
}}
}
public class Square extends Rectangle {public
square (int w, int h) {
super (W, h);
}
public int Getarea () {return
width*width
}}
}
public class Test {public
static void Main (string[] args) {
Rectangle Rectangle = new Rectangle (a);
Square rectangle = new Square (a);
System.out.println ("Area:" +rectangle.getarea ());
}
}
If we replace the rectangular class rectangle with square squares, the area we are seeking is not correct, because we have overridden the Getarea method of the parent class when we inherited it. This is against the Richter replacement principle.
Of course, here is just an example, in the actual project we will not modify this.
Summarize:
1. Try not to rewrite the parent class method, but to add your own unique method.
2. Inheritance has brought a lot of drawbacks as well as great convenience to program design. If a class is inherited by another class, when the class needs to be modified, all subclasses must be taken into account, and when the parent class is modified, all functions involving subclasses are likely to produce bugs.