About me

Gladson A
Software Engineer,Chennai.

Translate

Pages

Saturday 30 June 2012

this( ) and super( ) in constructor


this() in constructor



-The this() call in a constructor invokes the an other constructor with the corresponding parameter list within the same class.


-Java requires that any this() call must occur as the first statement in a constructor.

program:

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

super() in constructor:



-The super() statement invokes a constructor of the super class.


- The super statement must be the first statement of the constructor.

program 1:

(Note Carefully : Every constructor calls its superclass constructor) 

class a

{

a()

{
System.out.println("this is super class constructor");
}
}

public class b extends a

{

b(int b)

{
System.out.println("this is  main class parameterized constructor:"+b);
}

b()


this(12);

System.out.println("this is  main class constructor");
}

public static void main(String[] a)

{
b s=new b();
}


}

output:

this is super class constructor
this is  main class parameterized constructor:12
this is  main class constructor

Constructor chain:

program 2:

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



-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.

                                       Ask your Forums and Questions in the Comment box (mention your main Id).

1 comments: