You might hear about this term sometime, somewhere and now you trying to implement it in your code. But you couldn’t see what is the sense of using this method. This post may help you.

Consider the snippet below:

package com.samderlust;
public class Doctor {
    private String firstName;
    private String lastName;
    private String specialty;
    
    //constructor number 1
    public Doctor() {
        this.firstName = "Tom";
        this.lastName = "Jackson";
        this.specialty = "Psychology";
    }

    //constructor number 2
    public Doctor(String specialty) {
        this.firstName = "Tom";
        this.lastName = "Jackson";
        this.specialty = specialty;
    }
    
    //constructor number 3
    public Doctor(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.specialty = "Psychology";
    }

    //constructor number 4
    public Doctor(String firstName, String lastName, String specialty) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.specialty = specialty;
    }

    @Override
    public String toString() {
        return "Doctor{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", specialty='" + specialty + '\'' +
                '}';
    }

    public static void main(String[] args) {
        Doctor tom = new Doctor(); //use constructor number 1
        Doctor justin = new Doctor("Justin", "Pepper"); //use constructor number 2
        Doctor tom2 = new Doctor("genneral"); //use constructor number 3
        Doctor valak = new Doctor("Valak", "Micheal", "Psychology"); //use constructor number 4
        System.out.println(tom);
        System.out.println(justin);
        System.out.println(tom2);
        System.out.println(valak);
    }
}

In the main method, we created 4 instances of the class Doctor by using 4 respective constructor function that we’ve defined in the class. After creating these instances, we print all of them to the console and the result is as below:

Doctor{firstName='Tom', lastName='Jackson', specialty='Psychology'}
Doctor{firstName='Justin', lastName='Pepper', specialty='Psychology'}
Doctor{firstName='Tom', lastName='Jackson', specialty='genneral'}
Doctor{firstName='Valak', lastName='Micheal', specialty='Psychology'}

Why using constructor chaining?

Now, if you take the time to look at all the constructor, there’are a lot of repeating code where we keep on doing the same thing of assigning the default values for the fields of the class. Imagine if you have a big class which has 20, 30 or 100 of fields inside it, it would be a mess if you writing the constructor this way.

In programming, if you repeat your code too many times, it’s not effective and makes your code looks so messy. The redundancy of code writing will make you lose some marks in the interview for sure.

How to chain the constructors?

Let’s refactor the code above by using constructor chaining.

//constructor number 1
public Doctor() {
    this("Tom", "Jackson", "Psychology");
}

//constructor number 2
public Doctor(String specialty) {
    this("Tom", "Jackson", specialty);
}

//constructor number 3
public Doctor(String firstName, String lastName) {
    this(firstName, lastName, "Psychology");
}

//constructor number 4
public Doctor(String firstName, String lastName, String specialty) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.specialty = specialty;
}

Now, it looks cleaner and shorter. But what do I mean by calling it “constructor chaining”?

Doctor tom = new Doctor(); //use constructor number 1

In the precedent code, we using the constructor number 1, since the constructors is chained, the constructor 1 will call the constructor number 4 inside it, and pass the respective parameters.

//constructor number 1
public Doctor() {
    this("Tom", "Jackson", "Psychology");
}

Respectively, the code below used the constructor number 2 that accepts one parameter from outside which is the specialty. Two more default value for fistName and lastName are also passed inside of it.

Doctor justin = new Doctor("Justin", "Pepper"); //use constructor number 2

//constructor number 2
public Doctor(String specialty) {
    this("Tom", "Jackson", specialty);
}

This 2nd constructor is also chained to the 4th one so that it will pass in all the values with the respective order.

As you can see, the order of parameters in constructors number 1,2,3 have to be the same with the order in the 4th constructor. They do. Because all the first 3 constructors call the 4th one when you use them to initiate an instance.

Basically, by using constructor chaining, you call a constructor inside another constructor.

Now, your time to take practice.

 

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here