Constructor Chaining in Java

Every constructor calls its superclass constructor. An implied super() is therefore included in each constructor which does not include either the this() function or an explicit super() call as its first statement. The super() statement invokes a constructor of the super class.
The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor.
The explicit super allows parameter values to be passed to the constructor of its superclass and must have matching parameter types A super() call in the constructor of a subclass will result in the call of the relevant constructor from the superclass, based on the signature of the call. This is called constructor chaining.
Below is an example of a class demonstrating constructor chaining using super() method.

class Cube {
    int length;
    int breadth;
    int height;

    public int getVolume() {
        return (length * breadth * height);
    }

    Cube() {
        this(10, 10);
        System.out.println("Finished with Default Constructor of Cube");
    }

    Cube(int l, int b) {
        this(l, b, 10);
        System.out
                .println("Finished with Parameterized Constructor having 2 params of Cube");
    }

    Cube(int l, int b, int h) {
        length = l;
        breadth = b;
        height = h;
        System.out
                .println("Finished with Parameterized Constructor having 3 params of Cube");
    }
}

public class SpecialCube extends Cube {
    int weight;

    SpecialCube() {
        super();
        weight = 10;
    }

    SpecialCube(int l, int b) {
        this(l, b, 10);
        System.out
                .println("Finished with Parameterized Constructor having 2 params of SpecialCube");
    }

    SpecialCube(int l, int b, int h) {
        super(l, b, h);
        weight = 20;
        System.out
                .println("Finished with Parameterized Constructor having 3 params of SpecialCube");
    }

    public static void main(String[] args) {
        SpecialCube specialObj1 = new SpecialCube();
        SpecialCube specialObj2 = new SpecialCube(10, 20);
        System.out.println("Volume of SpecialCube1 is : "
                + specialObj1.getVolume());
        System.out.println("Weight of SpecialCube1 is : " + specialObj1.weight);
        System.out.println("Volume of SpecialCube2 is : "
                + specialObj2.getVolume());
        System.out.println("Weight of SpecialCube2 is : " + specialObj2.weight);
    }
}

Output:
inished with Parameterized Constructor having 3 params of SpecialCube
Finished with Parameterized Constructor having 2 params of SpecialCube
Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20

The super() construct as with this() construct: if used, must occur as the first statement in a constructor, and it can only be used in a constructor declaration. This implies that this() and super() calls cannot both occur in the same constructor. Just as the this() construct leads to chaining of constructors in the same class, the super() construct leads to chaining of subclass constructors to superclass constructors.
if a constructor has neither a this() nor a super() construct as its first statement, then a super() call to the default constructor in the superclass is inserted.
Note: If a class only defines non-default constructors, then its subclasses will not include an implicit super() call. This will be flagged as a compile-time error. The subclasses must then explicitly call a superclass constructor, using the super() construct with the right arguments to match the appropriate constructor of the superclass.
Below is an example of a class demonstrating constructor chaining using explicit super() call.

class Cube
{
    int length;
    int breadth;
    int height;
    public int getVolume()
    {
        return (length * breadth * height);
    }
    Cube(int l, int b, int h)
    {
        length = l; breadth = b;
        height = h;
        System.out.println("Finished with Parameterized Constructor having 3 params of Cube");
    }
}
public class SpecialCube1 extends Cube
{
    int weight; SpecialCube1()
    {
        super(10, 20, 30); //Will Give a Compilation Error without this line weight = 10; } public static void main(String[] args) { SpecialCube1 specialObj1 = new SpecialCube1(); System.out.println("Volume of SpecialCube1 is : "+ specialObj1.getVolume()); } }
    }
}

 

Output:
inished with Parameterized Constructor having 3 params of Cube
Volume of SpecialCube1 is : 6000

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More