Constructor chaining in java is also a popular java interview question. Here we will see what is constructor chaining and also we will see a couple of examples of constructor chaining using this and super.
What is constructor chaining in java?
- Constructor chaining is a process of calling one constructor from another constructor.
- We use this keyword for constructor chaining if multiple constructors are defined in the same class.
- We use the super keyword for constructor chaining if multiple constructors are defined in the different class(all classes must be in the inheritance relationship).
Example of constructor chaining using this keyword?
We have simple class ConstructorChainingUsingThis.java where we have three constructors.
public class ConstructorChainingUsingThis { public String name; public int id; public ConstructorChainingUsingThis() { this("rakesh"); System.out.println("this is defualt constructor"); } public ConstructorChainingUsingThis(String name) { this("mohan", 2); this.name = name; System.out.println("name is in single parameter constructor " + name); } public ConstructorChainingUsingThis(String name, int id) { this.name = name; this.id = id; System.out.println("name is " + name + " Id is " + id); } public static void main(String[] args) { ConstructorChainingUsingThis chainingUsingThis = new ConstructorChainingUsingThis(); } }
Output is –
name is mohan Id is 2
name is in single parameter constructor rakesh
this is defualt constructor
Let’s see program execution in details –
- Program execution will start with main method line number 27.
- In line 28 we are creating an object of ConstructorChainingUsingThis class using the default constructor.
- As soon as the object will create constructor will be called, control will come to line 8.
- this(“rakesh”) will invoke the current class constructor, control will come to line 14. Did you notice line number 9 i.e System.out.println(“this is default constructor”) is not executed yet.
- this(“mohan”, 2) will invoke the current class constructor, control will come to line 22.
- So first it will print line 24, 16 and 9.
Example of constructor chaining using the super keyword.
- Before proceeding, it is recommended to know some basic concept related to super.
- If you don’t define any constructor in class compiler will insert default constructor with super, something like below.
//What do you write class Book { } // What compiler see class Book { Book() { super(); } }
Let’s see in below diagram, What do you write and what does compiler see –
Let’s see example –
class Book { Book() { System.out.println("default constructor"); } } class StoryBook extends Book { StoryBook(String name) { super(); System.out.println("name is " + name); } } class OldStoryBook extends StoryBook { OldStoryBook() { super("bookname1 "); } } public class ConstructorChainingUsingSuper { public static void main(String[] args) { OldStoryBook oldStoryBook = new OldStoryBook(); } }
Output is –
default constructor
name is bookname1
Let’s see program execution in details –
- Program execution will start with the main method.
- As soon as line 26 will execute, OldStoryBook class constructor will get invoked.
- Line 19 will get executed and it will call StoryBook class constructor.
- Line 11 will get executed and it will call Book class constructor.
Another example –
class Book { Book() { System.out.println("default constructor"); } } class StoryBook extends Book { } public class ConstructorChainingUsingSuper { public static void main(String[] args) { StoryBook storyBook = new StoryBook(); } }
What will happen when we run the above program.
- Program execution will start with main method.
- Line 15 will get executed and it will call StoryBook class default constructor(Don’t forget since we have not defined parametrized constructor JVM will insert default constructor and super()).
- super() inside StoryBook will call the super class constructor i.e Book class constructor and it will print
default constructor