Constructor chaining in Java is the process of calling one constructor from another constructor with in the same class or calling the parent class constructor from the child class.
So constructor chaining in Java can be done in two ways-
- When calling one constructor from another with in the same class. In this case this Keyword can be used to call constructors in chain.
- In case of inheritance when calling the parent class’ constructor from the child class. In this case super keyword can be used to call constructors.
How does constructor chaining help
Constructor chaining in Java helps with reducing the code redundancy by doing the task of initialization in one of the constructor. All the other constructors merely call that constructor in a chain for initialization.
Consider the scenario where you have 3 fields in your class and you want to give option to initialize all of them or two of them or only a single one or none. If you keep the initialization in all of the constructors then the code will look like as shown below.
Code without constructor chainingpublic class ConstrChaining { int a; double b; String name; ConstrChaining(){ } ConstrChaining(int a){ this.a = a; } ConstrChaining(int a, double b){ this.a = a; this.b = b; } ConstrChaining(int a, double b, String name){ this.a = a; this.b = b; this.name = name; } .. .. }
As you can see there is redundancy of initialization code with in the constructors. By using constructor chaining where one constructor calls another same code can be written as following.
Code with constructor chainingpublic class ConstrChaining { int a; double b; String name; ConstrChaining(){ this(0); } ConstrChaining(int a){ this(a, 0.0); } ConstrChaining(int a, double b){ this(a, b, null); } ConstrChaining(int a, double b, String name){ this.a = a; this.b = b; this.name = name; } public static void main(String[] args) { ConstrChaining cc = new ConstrChaining(); System.out.println("a- " + cc.a + " b- " + cc.b + " name- " + cc.name); ConstrChaining cc1 = new ConstrChaining(5, 7.8); System.out.println("a- " + cc1.a + " b- " + cc1.b + " name- " + cc1.name); ConstrChaining cc2 = new ConstrChaining(18, 13.45, "knpCode"); System.out.println("a- " + cc2.a + " b- " + cc2.b + " name- " + cc2.name); } }Output
a- 0 b- 0.0 name- null a- 5 b- 7.8 name- null a- 18 b- 13.45 name- knpCode
As you see now the initialization is done by the single constructor in the class, all the other constructors just call that constructor in a chain rather than doing the initialization themselves.
Constructor chaining in Java with inheritance
Constructor chaining in the case of inheritance is the process of calling the parent class constructor from the child class. Rather than initializing the fields of the parent class again, in the constructor of the child class you can call the constructor of the parent class using super keyword. This helps in reducing code duplication.
public class Area { int length; int width; Area(int length, int width){ this.length = length; this.width = width; } public static void main(String[] args) { Volume volume = new Volume(5,6,7); System.out.println("length-" + volume.length + " width-" + volume.width + " height-" + volume.height); } } class Volume extends Area{ int height; Volume(int length, int width, int height){ // Calling constructor of parent class super(length, width); this.height = height; } }Output
length-5 width-6 height-7
In this example you can see that the parent class constructor is called from the child class constructor using the super keyword to initialize the fields of the parent class.
Rules regarding constructor chaining with super
- If super() is used in a constructor to call the constructor of the parent class then it has to be the first statement in the constructor otherwise you will get compile time error "Constructor call must be the first statement in a constructor".
- If you don’t call the parent class constructor explicitly then the default no-arg constructor of each super class will be executed implicitly.
- In case of constructor chaining from child class to parent class (inheritance hierarchy) the order of calling the constructor is from parent class to child class.
Let’s see it with an example.
class A{ A(){ System.out.println("In the constructor of class A"); } } class B extends A{ int i; B(int i){ this.i = i; System.out.println("In the constructor of class B"); } } class C extends B{ C(int i){ super(i); System.out.println("In the constructor of class C"); } public static void main(String[] args) { C c = new C(5); } }
In this code A is the super class which is extended by class B which in turn is extended by class C.
When you create an object of class C, super class constructors will be called in chain starting from super class to child class giving the output as following.
In the constructor of class A In the constructor of class B In the constructor of class C
Also note that from the constructor of class B, constructor of class A is not called explicitly still it is called because the default no-arg constructor of the superclass will be executed implicitly.
That's all for the topic Constructor Chaining in Java. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Initializer Block in Java
- Constructor Overloading in Java
- Fail-fast And Fail-safe Iterators in Java
- Can we Start a Thread Twice in Java
- How to Sort ArrayList of Objects in Java
- Java Program to Check if The Given Strings Are Anagram or Not
- Introduction to YARN in Hadoop
- Spring Boot Stand Alone (non web) Application Example
No comments:
Post a Comment