define

One of the basic principles of object-oriented design. Richter’s rule of substitution says that wherever a base class can appear, a subclass must appear. LSP is the cornerstone of inheritance reuse. The base class can be reused only when the derived class can replace the base class and the function of the software unit is not affected. The derived class can also add new behaviors on the base class. Richter’s substitution principle is a complement to the open – close principle. The key step in implementing the “open – close” principle is abstraction. The inheritance relationship between base class and subclass is the concrete realization of abstraction, so the Richter substitution principle is the specification of the concrete steps to realize abstraction.

The principle of

The first point

A subclass must implement an abstract method of the parent class, but may not override a nonabstract (implemented) method of the parent class.

class Foo {
    public void cal(int num1, int num2) {
        int value = num1 + num2;
        System.out.println("Parent class calculation result:" + value);
    }
}

class Son extends Foo {
    public void cal(int num1, int num2) {
        int value = num1 - num2;
        System.out.println("Subclass calculation result:"+ value); } } class Cal{ public static void main(String[] args) { Foo foo = new Foo(); Foo. CAL (2, 1); Son son = new Son(); Son. CAL (2, 1); }}Copy the code

In class inheritance, a method defined by our parent class does not force its subclasses to follow the implementation rules of that method. A subclass can modify any method it inherits from its parent class. In this case, the parent class intended to define a method that adds two numbers, but the subclass inherited the method and changed it to subtraction, and succeeded. When a subclass does this, it breaks the entire inheritance system. When you try to replace a parent class with a subclass, you will find that normal functionality is now broken.

The second point

When a subclass needs to override a method in its parent class, the subclass method’s parameters (input parameters) are looser (broader) than the parameters entered by the parent method.

class Foo {
    public void method(List arrayList) {
        System.out.println("Superclass method execution");
    }
}

class Son extends Foo {
    public void method(ArrayList list) {
        System.out.println("Subclass method execution" );
    }
}

class Cal{
    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        Foo foo = new Foo();
        Son son = new Son();
        System.out.println("Result of call with superclass object:");
        foo.method(list);
        System.out.println("Replace a parent object with a subclass object call result"); son.method(list); }} // Prints the result of a call using a superclass object: superclass method execution replaces the superclass object with the result of a subclass object call, subclass method executionCopy the code

The original method is expected to be executed after the object is replaced, but the result has changed.

Modify the

class Foo {
    public void method(ArrayList arrayList) {
        System.out.println("Superclass method execution"); }} class Son extends Foo {public void method(List List) {system.out.println ();}} Class Son extends Foo {// Public void method(List List) {system.out.println ("Subclass method execution"); }}Copy the code

The third point

When overriding or implementing a superclass method, the return value of the method can be reduced, but not enlarged. Is:

 class Foo {
    public List getList() {
        return new ArrayList();
    }
}

class Son extends Foo {
    public ArrayList getList() {
        returnnew ArrayList(); }}Copy the code

Example:

class Foo {
    public ArrayList getList() {
        return new ArrayList();
    }
}

class Son extends Foo {
    public List getList() {
        returnnew ArrayList(); }}Copy the code

If we try to enlarge, override, or implement a return value from a superclass method in a subclass, the code will fail even the basic compiler.

Fourth,

Subclasses can have their own unique methods or attributes

class Foo {
    public void cal(int num1, int num2) {
        int value = num1 + num2;
        System.out.println("Parent class calculation result:" + value);
    }
}

class Son extends Foo {
    public void cal(int num1, int num2) {
        int value = num1 - num2;
        System.out.println("Subclass calculation result:" + value);
    }
    public void cal2(int num1, int num2) {
        int value = num1 + num2 +num2;
        System.out.println("Subclass calculation result:"+ value); }}Copy the code

conclusion

From the above description, WE believe that we have a basic concept of The Richter substitution principle, in fact, it tells us what problems to pay attention to in inheritance and what rules to follow.

In practice, however, we often violate this principle, and while there is nothing particularly wrong on the surface, doing so can greatly increase the error rate of the code. When we write code, we not only need to consider how to implement this function, but also need to consider the robustness of the program and later extension and migration. Only by doing so can we make our programs better.

Personal blog Tencent Cloud community brief book CSDN official number: