Constructor Chaining in Java With Examples
Constructor Chaining in Java With Examples
Constructor chaining is the process of calling one constructor from another constructor with respect to current
object.
One of the main use of constructor chaining is to avoid duplicate codes while having multiple constructor (by
means of constructor overloading) and make code more readable.
Within same class: It can be done using this() keyword for constructors in the same class
From base class: by using super() keyword to call the constructor from the base class.
Constructor chaining occurs through inheritance. A sub-class constructor’s task is to call super class’s constructor
first. This ensures that the creation of sub class’s object starts with the initialization of the data members of the
superclass. There could be any number of classes in the inheritance chain. Every constructor calls up the chain till
the class at the top is reached.
This process is used when we want to perform multiple tasks in a single constructor rather than creating a code
for each task in a single constructor we create a separate constructor for each task and make their chain which
makes the program more readable.
Dr Bharti Sharma
Java
class Temp
// default constructor 1
Temp()
// calls constructor 2
Dr Bharti Sharma
this(5);
// parameterized constructor 2
Temp(int x)
// calls constructor 3
this(5, 15);
System.out.println(x);
}
Dr Bharti Sharma
// parameterized constructor 3
Temp(int x, int y)
System.out.println(x * y);
new Temp();
Output:
75
Dr Bharti Sharma
5
The Default constructor
Rules of constructor chaining :
1. The this() expression should always be the first line of the constructor.
2. There should be at-least be one constructor without the this() keyword (constructor 3 in above
example).
3. Constructor chaining can be achieved in any order.
class Temp
{
Dr Bharti Sharma
// default constructor 1
Temp()
System.out.println("default");
// parameterized constructor 2
Temp(int x)
this();
System.out.println(x);
Dr Bharti Sharma
// parameterized constructor 3
Temp(int x, int y)
this(5);
System.out.println(x * y);
{
Dr Bharti Sharma
Output:
default
5
80
NOTE: In example 1, default constructor is invoked at the end, but in example 2 default constructor is invoked
at first. Hence, order in constructor chaining is not important.
class Base
String name;
// constructor 1
Base()
this("");
}
Dr Bharti Sharma
// constructor 2
Base(String name)
this.name = name;
+ " of base");
{
Dr Bharti Sharma
// constructor 3
Derived()
"of derived");
// parameterized constructor 4
Derived(String name)
super(name);
Dr Bharti Sharma
"constructor of derived");
}
Dr Bharti Sharma
Output:
Calling parameterized constructor of base
Calling parameterized constructor of derived
Note : Similar to constructor chaining in same class, super() should be the first line of the constructor as super
class’s constructor are invoked before the sub class’s constructor.
class Temp
System.out.println("init block");
// no-arg constructor
Temp()
System.out.println("default");
Temp(int x)
Dr Bharti Sharma
System.out.println(x);
// constructor.
new Temp();
new Temp(10);
Output:
init block
default
init block
10
NOTE: If there are more than one blocks, they are executed in the order in which they are defined within the
same class. See the ex.
Example :
Java
class Temp
Dr Bharti Sharma
System.out.println("init");
Temp()
System.out.println("default");
Temp(int x)
System.out.println(x);
Dr Bharti Sharma
System.out.println("second");
new Temp();
new Temp(10);
}
Dr Bharti Sharma
Output :
init
second
default
init
second
10
This article is contributed by Apoorva singh. If you like GeeksforGeeks and would like to contribute, you can
also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See
your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
Like175
Previous
Copy Constructor in Java
Next