Here is the example program using super keyword.
Class add
{
double weidth,length,height;
add(add ob)
{
weidth =Ob.weidth;
length= Ob.length;
heigth= Ob.height;
}
add(double w,double l,double h)
{
weidth= w;
length= l;
heigth= h;
}
Double volume()
{
return weidth*length*height;
}
}
Class subtract extends add
{
Double bredth;
Subtract(double w,double l,double h,double b)
{
Super(w,l,h);
bredth=b;
}
}
Class demo
{
Public static void main(string args[])
{
Subtract ob1=new subtract(12,34,56,67.8);
add ob2=new add();
Double vol;
Vol=ob1.volume();
System.out.println(“volume of it :=”+vol);
Ob2=ob1; //assigning ob1 reference (of subclass) to the add reference which is super class
Vol=ob2.volume(); //this statement is ok, because volume is defined in add class.
System.out.println(“the volume is :”+vol);
System.out.println(“bredth of the ob1 is :”+ob2.bredth);
}
}
THE SECOND USE OF SUPER
The second form of super act somewhat like this, expect that it always reffers to the superclass of the sunclass in which it is being used. It has following format
Super.member
Here member can be either method or an instant variable.
The example program based on this second use of super is as follows
Char a
{
int i;
}
Class b extends a
{
int i;
b(int a,int b)
{
super.i=a;
i=b;
}
Void show()
{
System.out.println(“the value of I in superclass is:”+super.i);
System.out.println(“the value of I in subclass is:”+i);
}
Class suuper
{
Static public void main(string args[])
{
b ob1=new b(12,45);
ob1.show()
}
}
MULTILEVEL HIERARCHY
Till now what we studied that one class inherites the properties of another in order to run successfully.Now we will see that there can be many classes which can be subclass for their above classes.we can tell those classes as multilevel classes. Let’s elaborate these points by some example programs.
class first
{
int i,j,k;
first(first f)
{
i =f.i;
j=f.j;
k=f.k;
}
first(int a,int b,int c)
{
i=a;
j=b;
k=c;
}
void show()
{
System.out.println(“i=”+i+”j=”+j+”k=”+k);
}
class second extend first
{
int l;
second(second f)
{
super(f);
l=s.l;
}
second(int a)
{
super(I,j,k);
l=a;
}
class third extends second
{
int m;
third(third f)
{
super(f);
m=t.m;
}
third(int a)
{
Super(i,j,k,l);
m=a;
}
class final
{
Public static void main(String args[])
{
third ob1=new third(10,20,30,40,50);
third ob2=new third(25,15,35,45,56);
ob1.show();
ob2.show();
}
}
Here we found that the class name called third is the subclass of second class which is ultimately the subclass of first class i.e third class can inherite the properties from both the classes.