Using super to Call Superclass Constructors
A subclass could call a constructor method defined through its superclass via use of the following form of super which is
super(parameter - list);
In the form a parameter-list specifies any parameters required through the constructor in the superclass. A super ( ) must always be the first statement executed within a subclass' constructor.
To look how super ( ) is used, let consider this improved version of the BoxWeight( ) class:
//BoxWeight now uses super to initialize its Box attributes.
class BoxWeight extends Box {
double weight; // weight of box
// initialize width, height and depth using super ( )
BoxWeight (double w, double h, double d, double m) {
super (w,h,d); // call superclass constructor
weight = m;
}
}
Here BoxWeight( ) calls super ( ) along with the parameters w, h and d. This causes the Box( ) constructor to be called, that initializes height, width and depth using these values. A BoxWeight no longer initializes these values itself. It only requires initializing the value unique to it: weight. This leaves Box free to make these values private if desired.
Within the preceding example, super ( ) was called along with three arguments. Because constructors could be overloaded, super( ) could be called using any form defined through the superclass. A constructor executed will be the one which matches the arguments. For instance here is a full implementation of BoxWeight which gives constructors for the several ways in which a box can be constructed. In each case, super ( ) is called using the appropriate arguments. For instance, here is a complete implementation of BoxWeight which provides construction for the several ways in which a box can be constructed. Within each case, super ( ) is called using the suitable arguments. Remember that width, height and depth have been made private inside Box.
// A complete implementation of BoxWeight.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box (Box Ob) {
width = Ob.width;
height = Ob.height;
depth = Ob.depth;
}
// when all dimensions specified constructor used
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// when no dimensions specified constructor used
Box( ) {
width =-1; //use -1 to indicate
height =-1; // an uninitialized
depth = -1; //box
}
// when cube is created a constructor used
Box(double len) {
width = height = depth = len;
}
//compute and return volume
double volume( ) {
return width *height * depth;
}
}
// BoxWeight now full implements all constructors
class BoxWeight extends Box {
double weight; // weight of box
// construct clone of an object
BoxWeight (BoxWeight Ob) { // pass object to constructor
super(Ob);
weight = Ob.weight;
}
// when all parameters are specified using constructor
BoxWeight(double w, double h, double d, double m) {
super(w,h,d); // call superclass constructor
weight = m;
}
// default constructor
BoxWeight ( ) {
super( );
weight = -1;
}
// when cube is created constructor used
BoxWeight (double len, double m) {
super(len);
weight = m;
}
}
class DemoSuper {
public static void main(String args[ ]) {
BoxWeight mybox1 = new BoxWeight(20, 10, 34, 34.3);
BoxWeight mybox2 = new BoxWeight(3, 4, 2, 0.076);
BoxWeight mybox3 = new BoxWeight ( ); // default
BoxWeight mycube = new BoxWeight (3, 2);
BoxWeight myclone = new BoxWeight(mybox1);
double vol;
vol = mybox1.volume( ) ;
system.out.println("Volume of mybox1 is "+ vol);
system.out.println("Weight of mybox1 is " + mybox1.weight);
system.out.println( ) ;
vol = mybox2.volume( ) ;
system.out.println("Volume of mybox2 is "+ vol);
system.out.println("Weight of mybox2 is " + mybox2.weight);
system.out.println( ) ;
vol = mybox3.volume( ) ;
system.out.println("Volume of mybox3 is "+ vol);
system.out.println("Weight of mybox3 is " + mybox3.weight);
system.out.println( ) ;
vol = myclone.volume( ) ;
system.out.println("Volume of myclone is "+ vol);
system.out.println("Weight of myclone is " + myclone.weight);
system.out.println( ) ;
vol = mycube.volume( ) ;
system.out.println("Volume of mycube is "+ vol);
system.out.println("Weight of mycube is " + mycube.weight);
system.out.println( ) ;
}
}
This program generates the output:
Volume of mybox1 is 2000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
Volume of mybox3 is -1.0
Weight of mybox3 is -1.0
Volume of myclone is 3000.0
Weight of myclone is 34.3
Volume of mycube is 27.0
Weight of mybox1 is 2.0
Pay special attention to that constructor within BoxWeight( );
// construct clone of an object
BoxWeight(BoxWeight Ob) { // pass object to constructor
super(Ob);
weight = Ob.weight;
}
Note that super ( ) is called along with an object of type BoxWeight - not of type Box. That still invokes the constructor Box (Box ob). As mentioned previously, a superclass variable could be used to reference any object derived from that class. Therefore, we are able to pass a BoxWeight object to the Box constructor. Of course, Box only knowledge of its own members.
Let's review the key concepts behind super ( ). Whenever a subclass calls super( ), it is calling the constructor of its instant superclass. Therefore, super( ) always refers to the superclass immediately above the calling class. This is true even in a multileveled hierarchy. Also, super( ) must always be the first statement executed within a subclass constructor.