Java Constructors - Order of execution in an inheritance hierarchy

后端 未结 5 1216
予麋鹿
予麋鹿 2020-12-15 17:49

Consider the code below

  class Meal {
    Meal() { System.out.println(\"Meal()\"); }
  }

  class Bread {
    Bread() { System.out.println(\"Bread()\"); }
          


        
5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 18:22

    even though their constructors where called after.

    Not after, here how construstor method looks like to compiler :

    public Sandwich(){
        super();// note this calls super constructor, which will call it's super and so on till Object's constructor
        //initiate member variables
        System.out.println("Sandwich()");
    }
    

提交回复
热议问题