OOPS/JAVA UNIT 3 Uses of super keyword
by hkesavaraju on Apr.02, 2010, under Java
3.7 Uses of super keyword:
(i) To avoid sub class constructor to explicitly initialize the super class instance variables and also to avoid duplicate code in its super class.
(ii) There will be times to create super class that keeps the details of its implementation to itself. In that case, the sub class is not allowed to access super class variables directly or super class initialize those variables on its own.
(iii) Java provides a solution in which a sub class refers to its immediate super class using super keyword.
Super has two general forms.
3.7.1 First form: The first form is used to call a constructor method defined in its super class. It’s form is super(parameter-list);
where parameter-list specifies the parameters of the constructor in the super class. It should be always first statement executed inside the subclass constructor.
See how super() is used in the following example:
class Box
{
double w,h,d;
Box(Box ob) // object is passed to constructor
{
w=ob.w;
h=ob.h;
d=ob.d;
}
Box( double w1,double h1,double d1)
{
w=w1;
h=h1;
d=d1;
}
Box()
{
w=-1;
h=-1;
d=-1;
}
Box(Double len)
{
w=h=d=len;
}
double volume()
{
return w*h*d;
}
}
class BoxWeight extends Box
{
double m;
BoxWeight(double w1,double h1,double d1,double wg)
{
super(w1,h1,d1);
m=wg;
}
BoxWeight(BoxWeight ob)
{
super(ob);
m=ob.m;
}
BoxWeight(double len,double w1)
{
super(len);
m=w1;
}
BoxWeight()
{
super();
m=-1;
}
}
class demo
{
public static void main(String args[])
{
BoxWeight m1=new BoxWeight(2,3,4,6.0);
BoxWeight m2=new BoxWeight(10,20,30,8.0);
BoxWeight m3=new BoxWeight();
BoxWeight m4=new BoxWeight(3,2);
BoxWeight m5=new BoxWeight(m1);
double vol;
vol=m1.volume();
System.out.println(“volume of object m1 is”+vol);
System.out.println(“weight of object m1 is”+m1.m);
vol=m2.volume();
System.out.println(“volume of object m2 is”+vol);
System.out.println(“weight of object m2 is”+m2.m);
vol=m3.volume();
System.out.println(“volume of object m3 is”+vol);
System.out.println(“weight of object m3 is”+m3.m);
vol=m4.volume();
System.out.println(“volume of object m4 is”+vol);
System.out.println(“weight of object m4 is”+m4.m);
vol=m5.volume();
System.out.println(“volume of object m5 is”+vol);
System.out.println(“weight of object m5 is”+m5.m);
}
}
Output:
volume of object m1 is 24.0 weight of object m1 is 6.0 volume of object m2 is 48000.0 weight of object m2 is 8.0 volume of object m3 is -1.0 weight of object m3 is 1.0 volume of object m4 is 27.0 weight of object m1 is 2.0 volume of object m1 is 24.0 weight of object m1 is 6.0
In this, The class demo which has main() creates m1 object for sub class BoxWeight , the first statement super(w1,h1,d1) calls Box() constructor that takes three arguments such as w1,h1,d1 and are initialized to w, h & d respectively.
The second statement creates m2 for BoxWeight class, in which the first statement super(w1,h1,d1) calls Box() constructor that takes three double arguments.
The third statement creates object m3 for BoxWeight , in Which BoxWeight constructor has first statement super() which calls Box() constructor that takes no arguments.
The fouth statement crates object m4 for BoxWeight, in which BoxWeight constructor has
super(len) that calls Box() constructor that takes one double argument.
The fifth statement creates object m5 for BoxWeight, in which BoxWeight constructor has first statement super(ob) calls Box constructor that takes Box reference as an argument.
Second form:
This second form acts some what like this, except it always refer to the super class of the sub class in which it is used. It’s general form is
super.member where member can be either a method or an instance variable.
This second form can be used in most situations in which member names of a sub class hides same name in the super class.
The following program illustrates this second form:
class A
{
int i;
}
class B extends A
{
int i;
B(int a, int b)
{
super.i=a;
i=b;
}
void show()
{
System.out.println(“i in super class is”+super.i);
System.out.println(“i in sub class is”+i);
}
}
class superdemo
{
public static void main(String args[])
{
B ob=new B(1,2);
ob.show();
}
Output:
i in super class is 1
i in sub class is 2
Note: This second form of super is used to avoid the hiding of super class variables.