Constructor Overloading in Java

Like methods, constructors can also be overloaded. Since the constructors in a class all have the same name as the class, />their signatures are differentiated by their parameter lists. The above example shows that the Cube1 constructor is overloaded one being the default constructor and the other being a parameterized constructor.
It is possible to use this() construct, to implement local chaining of constructors in a class. The this() call in a constructorinvokes the an other constructor with the corresponding parameter list within the same class. Calling the default constructor to create a Cube object results in the second and third parameterized constructors being called as well. Java requires that any this() call must occur as the first statement in a constructor.
Below is an example of a cube class containing 3 constructors which demostrates the this() method in Constructors context:

public class Cube2 {
    int length;
    int breadth;
    int height;

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

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

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

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

    public static void main(String[] args) {
        Cube2 cubeObj1, cubeObj2;
        cubeObj1 = new Cube2();
        cubeObj2 = new Cube2(10, 20, 30);
        System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
        System.out.println("Volume of Cube2 is : " + cubeObj2.getVolume());
    }
}

public class Cube2 {
    int length;
    int breadth;
    int height;

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

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

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

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

    public static void main(String[] args) {
        Cube2 cubeObj1, cubeObj2;
        cubeObj1 = new Cube2();
        cubeObj2 = new Cube2(10, 20, 30);
        System.out.println("Volume of Cube1 is : " + cubeObj1.getVolume());
        System.out.println("Volume of Cube2 is : " + cubeObj2.getVolume());
    }
}

 

Output :
Finished with Parameterized Constructor having 3 params
Finished with Parameterized Constructor having 2 params
Finished with Default Constructor
Finished with Parameterized Constructor having 3 params
Volume of Cube1 is : 1000
Volume of Cube2 is : 6000

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More