Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
constructor
chaining

How to chain constructors in Java

Educative Answers Team
svg viewer

What is constructor chaining?

The process of invoking a sequence of constructors upon initialization of a class object is called constructor chaining. Constructor chaining is useful when you want to invoke multiple constructors, one after another, by initializing only one instance.

Constructor chaining comes in handy when we are dealing with inheritance. When an instance of a derived class is initialized, the constructors of all the inherited classes are first invoked and then, in the end, the constructor of the calling class is invoked. You can artificially invoke a constructor of either the calling class or the inherited class using:

  • this() to invoke a constructor of the calling class
  • super() to invoke a constructor of the inherited class

Code

This example will demonstrate constructor chaining in a single class.

class ExampleClass
{
  // default constructor
  ExampleClass()
  {
    this(20);
    System.out.println("Default constructor called");
  }

  // parameterized constructor
  ExampleClass(int x)
  {
    System.out.println("Parameterized constructor called");
  }

  // main
  public static void main(String args[]) 
  { 
      // initializes the instance of example class
      ExampleClass my_example = new ExampleClass(); 
  } 
}

In this example, the new instance, my_example, is initialized without passing any parameters. Therefore, the call goes to the default constructor, ExampleClass(), first. In the default constructor, the call is redirected to the parameterized constructor using the this() method. Following the redirected call, the block inside the parameterized constructor is executed and returns back to the default constructor (aka to line 7 right after the this() method call). Finally, the rest of the statements in the default constructor are executed and the object is successfully initialized. The flowchart below helps to visualize the calling sequence:

svg viewer

Code

This example will demonstrate constructor chaining in an inherited class:

class Base
{
  // default constructor
  Base()
  {
    this(10);
    System.out.println("Base class default constructor called");
  }

  // parameterized constructor
  Base(int x)
  {
    System.out.println("Base class parameterized constructor called");
  }
}

class Derived extends Base
{
  // default constructor
  Derived()
  {
    this(20);
    System.out.println("Derived class default constructor called");
  }

  // parameterized constructor
  Derived(int x)
  {
    super();
    System.out.println("Derived class parameterized constructor called");
  }
}

class MainProgram
{
  // main
  public static void main(String args[]) 
  { 
      // initializes the instance of example class
      Derived my_example = new Derived(); 
  } 
}

In this example, again, the new instance my_example of the Derived class is initialized without passing any parameters. Therefore, the call goes to the default constructor of the Derived class (Derived()), first. The default constructor calls the this() method and redirects the call to the parameterized constructor, Derived(int x). In the parameterized constructor the Base class’s default constructor is called using the super() method.

NOTE: during inheritance, even if super() is not called in the constructor of the derived class, it is implicit that it is called by the compiler. The code will perform in the exact same manner if you comment out line 29.

The default constructor of the Base class invokes its parameterized class using this(). All the statements in the parameterized constructor of the Base class are executed and then it returns to the default constructor of the Base class. All the statements in the default constructor of the Base class are executed and then returned to the parameterized constructor of the Derived class. All the statements in the parameterized constructor of the Derived class are executed and returned to the default constructor of the Derived class. Finally, all the statements in the default constructor of the Derived class are executed and the object is initialized successfully. Here is a flowchart to help you visualize the calling sequence:

svg viewer

CAUTION: You can not call the this() and super() methods in the same constructor block.

RELATED TAGS

java
constructor
chaining
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring