Constructor Chain in Java

I have little doubt regarding the output of the constructor chain program shown 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);
    }
}

CONCLUSION:

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

Confident that OutPut says that “1000,” “10,” “2000,” and “20” are successful?

In the main class, we created two objects:

SpecialCube specialObj1 = new SpecialCube();
SpecialCube specialObj2 = new SpecialCube(10, 20);

First, with the parameters “No parameters” and “with two parameters”, the first cube of the constructor () with “No Parameter” has only two values this(10,10), and one with “two parameters” has values

Cube(int l, int b) 
    {this(l, b, 10);}

I do not understand how "Air" is generated.

Volume of SpecialCube1 is : 1000
Weight of SpecialCube1 is : 10
Volume of SpecialCube2 is : 2000
Weight of SpecialCube2 is : 20

Please help me!

Thanks David

+3
source share
6 answers

, . specialObj1 , 1 , 3 . 10 . , 10 * 10 * 10 = 1000;

. , 3-arg. , , (...), , . . , = 10 .

2-arg, 10 20, 10 * 20 * 10 = 2000. ( 10 , 2args 3args.) 3-args, 2-args .

, .

+3

SpecialCube(), ():

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

, (l,b,h) = (10,10,10)

+7

, . :

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

, 1000 10 ( SpecialCube ).

:

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

20 SpecialCube(int l, int b, int h) , 2000 20.

, - , , , .

+4

,

1: "SpecialCube specialObj1 = SpecialCube();" "SpecialCube()" .

2: "Super()" "" SuperCube "SpecialCube".

3: " (10,10)" "", 2 "", "Cube (int l, int b)" (l = 10, b = 10).

4: " (l, b, 10) , № 3," (10,10,10) ", 3 " Cube (int l, int b, int h) ", # 3, " Cube (int l = 10, int b = 10, int h = 10) ".

5: , length = 10, widthth = 10, heigth = 10 "specialObj1", 1.

6: java "" 10 "SpecialCube()" . (. # 1)

. "SpecialCube()" ( "specialObj1".

7: System.out.println( " SpecialCube1 is:" + specialObj1.getVolume());, , java "" "getVolume()", "SpecialCube" "extends", . .

SpecialCube

8: 1 5, instanace "length = 10, widthth = 10, heigth = 10", "1000".

9: "System.out.println(" Weight of SpecialCube1 is: "+ specialObj1.weight); - " weight "" 10 "- # 6.

, . "specialObj2".

+1

:

SpecialCube1 , SpecialCube(), , , super(), Cube(), .

SpecialCube2 SpecialCube.

0

,

SpecialCube1 : 1000 // getVolume, 10,10,10, , l, b, h , ,

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;
    breadth = b;
    height = h;

SpecialCube1 : 10 // constr weight 10 (.. 1- )

Volume SpecialCube2: 2000 // the second time the getVolume function receives 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;

The weight of SpecialCube2 is: 20 // in the 2nd constr-weight, the value 20 is assigned (i.e. the 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;
0
source

Source: https://habr.com/ru/post/1774911/


All Articles