this() vs super() call

this() to call a constructor from another overloaded constructor in the same class. To call to this() can be used only in a constructor, and must be the first statement in a constructor changing, in other words when one constructor calls another constructor, and it will reduce duplicated code.

The only way to call a parent constructor is by calling super(). This calls the parent constructor.

The Java Compiler puts a default call to super() if we do not add it, and it is always the no-args super which is inserted by the compiler (constructor without arguments). The call to super() must be the first statement in each constructor. Even Abstract classes have constructors, although you can never instantiate an abstract class using the new keyword. An abstract class is still a super class, so its constructors run when someone makes an instance of a concrete subclass.

A constructor can have a call to super() or this() but never both.

Bad Constructors.

class People {
    private String name;
    private int age;

    public People() {
        this.name = "undefined";
        this.age = 0;
    }

    public People(String name) {
        this.name = name;
        this.age = 0;
    }

    public People(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
  • There is repeated code in every constructor. we are initializing variables in each constructor with some default values.

You should never write constructors like this.

Better Constructors with this()

class People {
    private String name;
    private int age;

    // first constructor
    public People() {
        // calls second constructor
        this("undefined");
    }

    // second constructor
    public People(String name) {
        // calls third constructor
        this("undefined", 0);
    }

    // third constructor
    public People(String name, int age) {
        // initialize variables
        this.name = name;
        this.age = age;
    }
}

The first constructor calls the second constructor and the second constructor calls the third constructor with this()

This is know as constructor chaining, the last constructor has the “responsibility” to initialize the variables.

Super() call

class ParentClass {
    private String parentName;
    private String parentCountry;

    public ParentClass(String parentName, String parentCountry) {
        this.parentName = parentName;
        this.parentCountry = parentCountry;
    }
}

class Me extends ParentClass {

    private String name;
    private String country;

    // first constructor
    public Me(String name, String country) {
        // calls second constructor
        this("Undefined", "Undefined", name, country);
    }

    // second constructor
    public Me(String parentName, String parentCountry, String name, String country) {
        // calls constructor from parent
        super(parentName, parentCountry);
        this.name = name;
        this.country = country;
    }
}

With super(), you can use the constructor of the parent class or super class.

Leave a Reply

Your email address will not be published.

ANOTE.DEV