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 having2 params of Cube");}Cube(int l, int b, int h) {length = l;breadth = b;height = h;System.out.println("Finished with Parameterized Constructor having3 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 having2 params of SpecialCube");}SpecialCube(int l, int b, int h) {super(l, b, h);weight = 20;System.out.println("Finished with Parameterized Constructor having3 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 SpecialCubeFinished with Parameterized Constructor having 2 params of SpecialCubeVolume of SpecialCube1 is : 1000Weight of SpecialCube1 is : 10Volume of SpecialCube2 is : 2000Weight 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 : 1000Weight of SpecialCube1 is : 10Volume of SpecialCube2 is : 2000Weight of SpecialCube2 is : 20``

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.

When you call `SpecialCube()`, the flow is like this (pseudo-code):

``````SpecialCube()
-> Cube()
-> Cube(10,10)
-> Cube(10,10,10)
l:=10, b:=10, h:=10
print message "Finished with Parameterized Constructor having 3 params of Cube"
print message "Finished with Parameterized Constructor having 2 params of Cube"
print message "Finished with Default Constructor of Cube"
weight:=10
``````

And at the end you have a cube constructed with `(l,b,h) = (10,10,10)`

Well, let's take one case at a time. For the first version, the constructor chain goes:

``````SpecialCube()
Cube()
Cube(10, 10)
Cube(10, 10, 10)
``````

So the cube ends up with a volume of 1000, and the default weight of 10 (as specified in the `SpecialCube` parameterless constructor).

In the second case, the constructor chain looks like this:

``````SpecialCube(10, 20)
SpecialCube(10, 20, 10)
Cube(10, 20, 10)
``````

and the weight is set to 20 by the `SpecialCube(int l, int b, int h)` parameterless constructor - so we have a volume of 2000 and a weight of 20.

If that still doesn't explain everything to you, please ask a very specific question - ideally about one of the cases, stating which bit you don't understand.

Here below the flow of execution,

step 1 : When " SpecialCube specialObj1 = new SpecialCube();" executed, the default constructor "SpecialCube()" will be called.

Step 2 : Now "Super()" will be excuted to call "SpecialCube's" super Class "Cube".

Step 3 : Now "this(10,10)" in super class "cube" will be executed, which will call constructor of 2 parameters from the same class "cube" , i.e. "Cube(int l,int b)" by passing parameters(l=10,b=10).

Step 4 : Now "this(l,b,10) will be executed with actual parameter passed in step#3 is "this(10,10,10)", which will call 3 parameterized constructor "Cube(int l, int b, int h)", with passing values from step#3, it will be like "Cube(int l=10,int b=10,int h=10)".

Step 5: instance variables like length = 10, breadth=10, heigth=10 for the the object "specialObj1" created at step # 1.

Step 6: Then java will assign "Weight" variable as 10 in the constructor "SpecialCube()". (Refer step # 1)

Now see method execution for object "SpecialCube()" (I am just considering only "specialObj1".

Step 7 : System.out.println("Volume of SpecialCube1 is : "+ specialObj1.getVolume()); , when this statement executed, java will call the super class "Cube's" "getVolume()" method , since subclass "SpecialCube" inheriates its by keyword "extends", refer below in your sublass.

public class SpecialCube extends Cube

step 8 : Step 1 thru 5, already assigned instanace variable as "length = 10, breadth=10, heigth=10", your getting volume as "1000".

step 9: "System.out.println("Weight of SpecialCube1 is : "+ specialObj1.weight); - this statement print the "weight" variable value as "10", because of step #6.

Hope, it will explain the reason.Try the same flow for object "specialObj2".

Break it down:

`SpecialCube1` is default constructed, so calls default constructor `SpecialCube()`, which in turn calls `super()`, which default constructs `Cube()`, that will explain the first two.

where as the `SpecialCube2` follows the proper constructor chain of `SpecialCube`.

here these outputs are generated because

volume of SpecialCube1 is : 1000 //when first time getVolume function is called ,it is getting 10,10,10, as arguments observe the following code where l,b,h values are assigned to length,breadth,height

``````Cube(int l, int b, int h) {
//l=10,b=10,h=10

System.out.println(l);
System.out.println(b);
System.out.println(h);
length = l;
height = h;
``````

Weight of SpecialCube1 is : 10 //in the first constr weight is assigned to 10 (ie 1st object)

Volume of SpecialCube2 is : 2000 // for the second time getVolume function is getting arguments from specialcube(3 argmted constr ie 10,20,10;

``````SpecialCube(int l, int b, int h)
{
//l=10,b=20,h=10
super(l, b, h);
System.out.println(l);
System.out.println(b);
System.out.println(h);
weight = 20;
``````

Weight of SpecialCube2 is : 20 //in the 2nd constr weight is assigned to 20 (ie 1st object)

SpecialCube(int l, int b, int h) { //l=10,b=20,h=10

``````    super(l, b, h);
System.out.println(l);
System.out.println(b);
System.out.println(h);
weight = 20;
``````

Top