The subclass constructor in Java calls the superclass constructor

Say up front:

Every class will have a default constructor (implicit), but if we rewrite a constructor, the default implicit constructor will be overwritten (it will be useless), unless we explicitly define the following , as shown in the following code:

Class Dog(){
    public Dog(){}  //显示声明默认构造器
    public Dog(int age, int weight){}
}

Topic:

Let me talk about the conclusion first: the constructor of the subclass will call the constructor of the parent class first.

 The output of the above code is:

 It can be seen that when we call the subclass constructor to new sonClass, it will first call the parent class constructor, and then call the subclass constructor.

Guess you like

Origin blog.csdn.net/SakuraMG/article/details/128433138