blog

Home / DeveloperSection / Blogs / Inheritance in Java: Object Creation Process

Inheritance in Java: Object Creation Process

Royce Roy 2890 16-May-2016

To better understand the object-creation process, let’s begin with a simple case of two-level inheritance, where we have just a base class and its derived class. When we create an instance of a derived class, it contains an object of its base class. This base class object is the same as the one we would have created by directly instantiating the base class itself. The derived class object is just a wrapper on this base class object. It is essential that the base class object be properly initialized. This can be ensured only if we know for sure that during the object-creation process the constructor of the base class is called—remember from our previous discussions that a class constructor has all the appropriate knowledge and privileges to perform its initialization. Thus, the compiler automatically inserts calls to the base class constructor in the derived class constructor provided we do not call super explicitly.

To explain the object-creation process, we will use the inheritance
hierarchy defined in our asset management system shown here.

Each of the three classes in this figure has an additional operation defined— and that is the class constructor. The printDescription method of the original Asset class is removed for simplicity. The constructor in all three cases does not take any arguments. The purpose of this no-arguments construct is to announce whenever the corresponding class is instantiated.

To keep things simple and focus on the aspects of object creation, we cover only the implementation code for the no-arguments constructor for each of these classes. The constructor prints a message to the user whenever the class is instantiated. The main program creates an object of SavingsAccount. This, in turn, creates objects of its superclasses, as we will see very shortly when we study the output of the program.

Inheritance in Java: Object Creation Process

Super Class- Asset
class Asset
 {
                     private int id;
                     private String type;
                     public Asset()
                             {
                             System.out.println("Creating Asset ...");
                     }
}

 Sub Class-Bank Account (Subclass of Asset class)

class BankAccount extends Asset {
                     private String bankName;
                     private int accountNumber;
                     private float balance;
                     public BankAccount() {
                             System.out.println("Creating BankAccount ...");
                     }
}
 SubClass –Saving Account (Subclass of SavingAccount)
class SavingsAccount extends BankAccount {
                     private float interestRate;  
                     public SavingsAccount() {
                             System.out.println("Creating SavingsAccount ...");
                     }
}
Test Class:
public class ObjectCreationProcess {

                     public static void main(String[] args) {
                             SavingsAccount tomSavingsAccount = new SavingsAccount();
                     }
}

 Output:

The main method simply creates an instance of the SavingsAccount class. After that, the instances of all its parent classes will be created. This can be seen from the program output, shown here:

Creating Asset ...

Creating BankAccount ...
Creating SavingsAccount ...

 

As we can see from the output, the Java runtime first calls the Asset class constructor, followed by BankAccount class constructor, and finally the SavingsAccount class constructor. This means that the object of the topmost superclass, also known as the base class, is constructed first, followed subsequently by all its subclasses in the order they are defined in the class hierarchy. This is also known as constructor chaining.

The runtime implicitly calls the no-arguments compiler-provided default constructor of each of the involved classes. As a matter of fact, the definition of the SavingsAccount constructor would be as follows:

public SavingsAccount() {
super();
System.out.println("Creating SavingsAccount ...");
}

 

Here, the compiler has added a call to super as the first statement in the constructor body.

This is a hidden call provided by the compiler. The compiler provides this as long as we do not write any explicit call to another constructor, which can be provided by either the super or this keyword. The use of the this keyword in calling another constructor defined within the same class


Updated 14-Mar-2018

Leave Comment

Comments

Liked By