I hava slight doubt regarding the Output of the Program of Constructor Chaining I showed Below:
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:
Finished 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 doubt is regarding the OutPut that how “1000”, “10”, “2000” & “20” are achiueved?
In the main Class We have created two Objects :
SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);
First with “No Parameters” and Second with “Two Parameters”, The First Constructor Cube() with “No Parameter” has only two values this(10,10) and the one with “Two Parameters” has the values
Cube(int l, int b)
{this(l, b, 10);}
I don’t understand how the Below OutPuts are generated.
Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20
Please Can anybody help me!
Thanks,
david
I think it is obvious.
specialObj1 is created during default constructor that invokes 1 argument constructor, that invokes 3 argument constructor. Each invocation sends 10 as value of cube dimension. So, volume is 10*10*10 = 1000;
weight is not method. It is field that is initialized in default and 3-arg constructor. Due to first thing the constructor does is call of this(…) it does not matter what value is assigned to this variable in other constructors. The first constructor in chain actually overrides all previously set values. This is a reason why weight = 10 for the first object.
Second object is created using 2-arg constructor that is called with parameters 10 and 20, so volume is 10*20*10 = 2000. (second 10 is set when 2args constructor calls 3args constructor.)
In case of second object the weight is set by 3-args constructor, because 2-args constructor does not override this value.
I hope this helps.