Constructor Chaining in Java
Use of this() and super() in Java Constructor Chaining
- Constructor chaining in Java is simply the act of one constructor calling another constructor via inheritance. This happens implicitly when a subclass is constructed: its first task is to call its parent's constructor method.
- But programmers can also call another constructor explicitly using the keywords this() or super(). The this() keyword calls another overloaded constructor in the same class; the super() keyword calls a non-default constructor in a superclass.
Learn Java - Java tutorial - Constructor Chaining In Java - Java examples - Java programs
Implicit Constructor Chaining
- Constructor chaining occurs through the use of inheritance. A subclass constructor method's first task is to call its superclass' constructor method.
- This ensures that the creation of the subclass object starts with the initialization of the classes above it in the inheritance chain.
- There could be any number of classes in an inheritance chain. Every constructor method calls up the chain until the class at the top has been reached and initialized.
- Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.
Note :
- This implicit call to the superclass is the same as if the subclass had included the super() keyword, i.e. super() is implicit here.
- If a no-args constructor is not included in the class, Java creates one behind the scenes and invokes it. This means that if your only constructor takes an argument, you must explicitly use a this() or super() keyword to invoke it.
- Consider this superclass Animal extended by Mammal:
- Now, let's instantiate the class Mammal:
- When the program runs, Java implicitly triggers a call to the superclass Animal constructor, then to the class' constructor.
- We're in class Animal's constructor
- We're in class Mammal's constructor
Explicit Constructor Chaining using this() or super()
- Explicit use of the this() or super() keywords allows you to call a non-default constructor.
- To call a non-args default constructor or an overloaded constructor from within the same class, use the this() keyword.
- To call a non-default superclass constructor from a subclass, use the super()keyword. For instance, if the superclass has multiple constructors, a subclass may always want to call a specific constructor, rather than the default.
- The call to another constructor must be the first statement in the constructor or will throw a compilation error.
- Consider the code below in which a new subclass, Carnivore, inherits from Mammal class which inherits from the Animal class, and each class now has a constructor that takes an argument.
- Here's the superclass Animal:
- The constructor now takes a name of type String as a parameter and that the body of the class calls this() on the constructor.
- Without the explicit use of this.name, Java would create a default, no-args constructor and invoke.
- Here's the subclass Mammal:
- Its constructor also takes an argument, and it uses super(name) to invoke a specific constructor in its superclass.
- Here's another subclass Carnivore. This inherits from Mammal:
- When run, these three code blocks would print:
- I'm executed first.
- I'm executed second.
- I'm executed last.
- When an instance of the Carnivore class is created, the first action of its constructor method is to call the Mammal constructor method.
- The first action of the Mammal constructor method is to call the Animal constructor method.
- A chain of constructor method calls ensure that the instance of the Carnivore object has properly initialized all the classes in its inheritance chain.