Use One: The first use of super is to refer to a super class variable or method which is hidden (variable) or overridden (method) in the super class.
Super can be written only in now static.
class A
{
int x = 10;
void met1()
{
System.out.println(x);
}
}
class Super1 extends A
{
int x=100;
void met1()
{
System.out.println("Super1");
System.out.println(super.x);
super.met1();
}
public static void main(String args[])
{
Super1 p = new Super1();
p.met1();
System.out.println(p.x);
}
}
OUTPUT : Super1
10
10
100
Use Two: The second use of super is to call a specific super class constructor from the subclass’s constructor by passing appropriate arguments.
The call to super should be in the first line of the sub class’s constructor.
class Area
{
int l, b;
Area(int a, int b)
{
l=a;
this.b = b;
}
void calArea()
{
int c = l * b;
System.out.println("Area : "+c);
}
}
class Volume extends Area
{
int h;
Volume(int a, int b, int c)
{
super(a,b);
h=c;
}
void calVolume()
{
int v = l * b * h;
System.out.println("Volume : " + v);
}
public static void main(String args[])
{
Volume x = new Volume(10,20,30);
x.calArea();
x.calVolume();
}
}
OUTPUT : Area : 200
Volume : 6000