Thinking about the construction method of child and parent classes (in-depth understanding of inheritance)

JAVA Beginner's Road (2021/9/24)
In-depth understanding of JavaSE error-prone points, summary:

  • About Inheritance:
    ------------ Introduction to Wrong Questions
class Parent {
    
    

public Parent(String s) {
    
    

System.out.print("B");

}

}

public class Son extends Parent {
    
    

public Son(String s) {
    
    

System.out.print("D");

}

public static void main(String[] args) {
    
    

new Son("");

System.out.print("C");

}

}

A. BD

B. DB

C. BDC

D. DBC

E. 编译失败

The answer is: E

  1. First of all, the construction method of the parent class must not be inherited by the subclass . The construction method of the subclass calls the construction method of the parent class. The key point is: the construction method of the subclass must call the construction method of the parent class anyway .
    !!! Understanding: For object-oriented, the meaning of the construction method itself is the attribute given to the object. At this point, it can be understood that the attribute of the subclass is a supplement to the parent class, but the attributes of the subclass and the parent class still have the same Differences, let’s take a simple example: (parent class) Dell Curry’s attributes include basketball talent, (subclass) Stephen Curry has added the talent of three-point shooting in addition to basketball talent in this attribute, That is to say, if the subclass simply inherits the properties of the parent class, it will deviate from the actual object-oriented meaning. In fact, the construction method of the subclass is based on the change of the construction method of the parent class , so it can be understood. Why does the subclass always call the constructor of the parent class, but the constructor of the parent class cannot be inherited by the subclass. ( When understanding java, an object-oriented programming language, it may be easier to understand in combination with real life )
public class person {
    
    
    public person(String s) {
    
    
        System.out.println("person有参构造方法");
    }


}
class Student extends person{
    
    

    public Student(String s) {
    
    
        System.out.println("Student有参构造方法");
    }
}

From the actual error code, it can be seen that if the construction method of the subclass and the parent class are as above, then the inheritance is not strongly reflected. Except for the expression of the extends keyword, it seems that these two are like two templates that have nothing to do with each other.

public class person {
    
    
    public person(String s) {
    
    
        System.out.println("person有参构造方法");
    }
    public person(){
    
    
        System.out.println("preson的无参构造方法");
    }
}
class Student extends person{
    
    
    public Student(String s) {
    
    
        super();
        System.out.println("person字符串参数的构造方法");
    }
    public Student(char s){
    
    
        System.out.println("student的字符参数的构造方法");
    }

}

The super() in the above string of codes makes the relationship between the two object templates closer, and it also fits the relationship between the child and the parent class in reality. But at the same time, there will be another problem. When the subclass executes the construction method whose formal parameter is char type, it can be seen that the construction method at this time has no relationship with the parent class.

  1. For a class, the class is just a template, and inheritance can only guarantee a certain relationship between the parent class and the subclass. For the actual created object, the properties of the object may not have much relationship with the parent class, for example : For the Student class to inherit the Person class, this ensures that there is a certain connection between the two templates, but when creating an object, the actual object cannot guarantee that its attributes must be related to the parent class, which can be understood from the mapping in real life. Xiaoming is Boy, but Xiaoming’s child does not necessarily have to be a boy, that is to say, inheritance guarantees that the two templates are related in one aspect, but not in all aspects

The above only represents the author's personal opinion. If there is any mistake, please forgive me and point it out in time. I hope it can help everyone

Guess you like

Origin blog.csdn.net/qq_52696089/article/details/120459246