The keyword super has two general forms :
The first calls the constructor of the super class.
The second is used access a member of the super class that has been hidden by member of a sub class.
First use of super
class Box
{ double width;
double height;
double depth;
Box(double w, double h, double d) //constructor
{ width = w ;
height = h ;
depth = d ;
}
double volume( )
{ return width*height*depth;
}
class Boxweight extends Box
{
double weight;
Boxweight( double w, double h, double d, double m)
{
super(w, h, d); //This will call the constructor of
//super class Box
weight = m;
}
}
Second use of super
This 2nd form of super is most applicable to situations in which member names of a subclass hide members by the same name in the
super class.
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 superclass : " +super.i );
//will
// print the value of i
// of super class A
System.out.println(" i in subclass : " +i ) ;
}
}
class UseSuper
{
public static void main( String args[] )
{
B Subob = new B(1,2);
Subob.show( );
}
}
Hence the output of the above program will be:
i in superclass : 1
i in subclass : 2
please ask if there are any queries....
No comments:
Post a Comment