Search This Blog

Wednesday 28 October 2015

Program to implement single inheritance using super keyword

class Box
{
float width,length;
Box()
{
width=0;
length=0;
}
Box(float w,float l)
{
width=w;
length=l;
}
Box(Box ob)
{
width=ob.width;
length=ob.length;
}
void area()
{
System.out.println("Area is: "+(width*length));
}
}

class BigBox extends Box
{
float depth;
BigBox()
{
super(0,0);
depth=0;
}
BigBox(Box ob,float d)
{
super(ob);
depth=d;
}
void volume()
{
System.out.println("Volume is: "+(width*length*depth));
}
}

class SuperEx
{
public static void main(String args[])
{
Box b=new Box(1.5f,2.5f);
BigBox bb=new BigBox(b,3.5f);
bb.area();
bb.volume();
}
}

OUTPUT:
C:\Program Files\Java\jdk1.7.0\bin>java SuperEx
Area is: 3.75

Volume is: 13.125

No comments:

Post a Comment