Inheritance (relationship of constructors in inheritance)

package Test;
/*
* All constructors in the subclass will access the constructor with empty parameters in the parent class by default
* Because the subclass will inherit the data in the parent class, and may also use the data of the parent class, so initialize it in the subclass Before, you must complete the initialization of the parent class data
* In fact, the first statement of each constructor is: super() by default, and the Object class is the top-level parent class
*/
public class ExtendsThree {

  public static void main(String[] args) {
    SonThree s=new SonThree();//Parent class constructor Subclass constructor
    FatherThree f=new SonThree();//Parent class constructor Subclass constructor
  }
}
class FatherThree{
  public FatherThree() {
    System.out.println("Parent class constructor");
  }
}
class SonThree extends FatherThree{
  public SonThree() {
  //super(); In fact, a super is hidden, super is the system Default added
    System.out.println("Subclass constructor");
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325024855&siteId=291194637