New Want to Write for Computaholics ? if Yes Contact us at "contact@computaholics.in"

Java program to create a Simple class to find out the Area and perimeter of rectangle and box using super and this

Write a java program to create a Simple class to find out the Area and perimeter of rectangle and box using super and this keyword:-

class rect
{
int l,b;
public rect(int l,int b)
{ this.l=l;
this.b=b;
}
public int area()
{
return l*b;
}
}
class box extends rect
{
int d;
public box(int l,int b,int d)
{
super(l,b);
this.d=d;
}
public int volume()
{
int vol = area()*d;
return vol;
}
public static void main(String args[])
{ int vol ,area;
System.out.println("derived object in derived reference");
rect r= new rect(10,20);
area=r.area();
System.out.println("area is "+area+"\n");
System.out.println("base object in base reference");
box b = new box(10,20,30);
vol=b.volume();
area=b.area();
System.out.println("area is "+area);
System.out.println("volume is "+vol+"\n");
System.out.println("derived object in base reference");
rect b1= new box(10,90,70);
area = b1.area();
//vol=b1.volume(); as with refernce of base class we can't call derived's
method
System.out.println("area is "+area);
//as super class doesn't know about the base class but reference can be
assigned
/*System.out.println("base object in derived reference");
box b2=(new rect (10,20));
vol = b2.area();
System.out.println("area is "+area);*/
r=b;
System.out.println(r.area());
System.out.println(r.volume());
}
}
Copyright © C Program | Java | OpenGL Programming | Hadoop at Computaholics