Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

order of constructor calls in multilevel inheritance in java [duplicate]

Tags:

//: c07:Sandwich.java // Order of constructor calls. // package c07; // import com.bruceeckel.simpletest.*;  import java.util.*;  class Meal {   Meal() { System.out.println("Meal()"); } }  class Bread {   Bread() { System.out.println("Bread()"); } }  class Cheese {   Cheese() { System.out.println("Cheese()"); } }  class Lettuce {   Lettuce() { System.out.println("Lettuce()"); } }  class Lunch extends Meal {   Lunch() { System.out.println("Lunch()"); } }  class PortableLunch extends Lunch {   PortableLunch() { System.out.println("PortableLunch()");} }  public class Sandwich extends PortableLunch { //  private static Test monitor = new Test();   private Bread b = new Bread();   private Cheese c = new Cheese();   private Lettuce l = new Lettuce();   public Sandwich() {     System.out.println("Sandwich()");   }   public static void main(String[] args) {     new Sandwich();    /*    monitor.expect(new String[] {       "Meal()",       "Lunch()",       "PortableLunch()",       "Bread()",       "Cheese()",       "Lettuce()",       "Sandwich()"     });     // */   } } ///:~ 

The output of this code is

Meal() Lunch() PortableLunch() Bread() Cheese() Lettuce() Sandwich()   

Since the fields in a class are created in the order they are declared, why don't

Bread() Cheese() Lettuce() 

come at the top of the above list?

Also, what is it trying to do in this code?

   monitor.expect(new String[] {       "Meal()",       "Lunch()",       "PortableLunch()",       "Bread()",       "Cheese()",       "Lettuce()",       "Sandwich()"     });   

At first I thought it was an anonymous class, but it doesn't look like it. Is it initializing a String array? Why doesn't it have the name for the String variable? Please tell me the name of the programming construct being used here.

like image 613
user13267 Avatar asked Jul 23 '13 09:07

user13267


People also ask

What is the order of constructor call in the multilevel inheritance?

For multiple inheritance order of constructor call is, the base class's constructors are called in the order of inheritance and then the derived class's constructor.

In which order execution of constructors in Java inheritance takes?

Answer: Order of execution of constructors in inheritance relationship is from base /parent class to derived / child class.

How constructors are called in multilevel inheritance in Java?

In multilevel inheritance, all the upper class constructors are executed when an instance of bottom most child class is created. In the above code, an instance of Student class is created and it invokes the constructors of College, Department and Student accordingly.

What is the order of execution of constructors in multilevel inheritance using an example program?

The order of execution of constructors in multilevel inheritance is; base class's constructor are executed first followed by execution of derived classes constructors.


1 Answers

The constructor:

public Sandwich() {     System.out.println("Sandwich()"); } 

Is translated by the compiler to:

public Sandwich() {     super();   // Compiler adds it if it is not explicitly added by programmer     // All the instance variable initialization is moved here by the compiler.     b = new Bread();     c = new Cheese();     l = new Lettuce();      System.out.println("Sandwich()"); } 

So, the first statement in a constructor is the chaining of super class constructor. In fact, the first statement in any constructor chains to the super class constructor, for that matter. That is why first the super class constructor PortableLunch is invoked, which again chains the call to it's super class constructor, due to the super() added by the compiler (remember?).

This chaining of constructor call is done till the class at the top of the inheritance hierarchy, thereby invoking the Object class constructor at the end.

Now, after each the super class constructor has been executed, and all the super class fields have been initialized, the immediate subclass constructor start the execution after the super() call. And then finally it comes back to the Sandwitch() constructor, which now initializes your 3 fields.

So, basically your fields are initialized at last, and hence they are printed at the end, just before Sandwitch() is printed.

Refer to JLS - §12.5 - Creation of New Class Instance for detailed explanation of the instance creation process.


As for your 2nd part of the question:

monitor.expect(new String[] {       "Meal()",       "Lunch()",       "PortableLunch()",       "Bread()",       "Cheese()",       "Lettuce()",       "Sandwich()"     });   

This code is creating an unnamed array, and initializing it some string literals at the same time. It is similar to the way you would create a named array:

String[] arr = new String[] { "rohit", "jain" }; 
like image 185
Rohit Jain Avatar answered Nov 07 '22 20:11

Rohit Jain