Learn the Use of this() and (super) in Java Constructor Chaining

Understanding Implicit and Explicit Constructor Chaining in Java

javascript code
ssuni / Getty Images

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.

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 that:

  • 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 (see below).

Consider this superclass Animal extended by Mammal:

class Animal {
// constructor
Animal(){
 System.out.println("We're in class Animal's constructor.");
}
}
class Mammal extends Animal {
//constructor
Mammal(){
 System.out.println("We're in class Mammal 's constructor.");
}
}

Now, let's instantiate the class Mammal:

public class ChainingConstructors {
 /**
* @param args
*/
public static void main(String[] args) {
Mammal m = new Mammal();
}
}

When the above program runs, Java implicitly triggers a call to the superclass Animal constructor, then to the class' constructor. The output, therefore, will be:

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.

Note that the call to another constructor must be the first statement in the constructor or Java 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: 

public class Animal
private String name;
public Animal(String name) // constructor with an argument
{
this.name = name;
System.out.println("I'm executed first.");
}
}
Note that 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

Here's the subclass Mammal:

public class Mammal extends Animal {
public Mammal(String name)
{
super(name);
System.out.println("I'm executed second");
}
}

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: 

public class Carnivore extends Mammal{
public Carnivore(String name)
{
super(name);
System.out.println("I'm executed last");
}
}

When run, these three code blocks would print:

I'm executed first.
I'm executed second.
I'm executed last.

To recap: When an instance of the Carnivore class is created, the first action of its constructor method is to call the Mammal constructor method. Likewise, 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.

Format
mla apa chicago
Your Citation
Leahy, Paul. "Learn the Use of this() and (super) in Java Constructor Chaining." ThoughtCo, Aug. 27, 2020, thoughtco.com/constructor-chaining-2034057. Leahy, Paul. (2020, August 27). Learn the Use of this() and (super) in Java Constructor Chaining. Retrieved from https://www.thoughtco.com/constructor-chaining-2034057 Leahy, Paul. "Learn the Use of this() and (super) in Java Constructor Chaining." ThoughtCo. https://www.thoughtco.com/constructor-chaining-2034057 (accessed March 28, 2024).