多层次调用父类构造方法:

  假设我们有这样一个层次结构:

  Object->Employee->Teacher->Professor


class Employee {

 public Employee() {
  System.out.println("Employee constructor called");
 }
}
class Teacher extends Employee {
 
 public Teacher() {
  System.out.println("Teacher constructor called");
 }

}
class Professor extends Teacher {

 public Professor() {
  System.out.println("Professor constructor called");
 }
}

public class Test {
 public static void main(String args[]) {
  Employee p = new Professor();
 }
}
 


  打印结果:


Employee constructor called
Teacher constructor called
Professor constructor called


  在创建Professor对象时(new Professor()),首先会找到该类的无参构造方法,然后首先调用super()方法,调用Teacher类的无参构造方法,接着再调用Employee的无参构造方法,后再调用Object的无参构造方法。后再打印出信息。