Professional Documents
Culture Documents
Practical No 18
Single inheritance:
class shape
return length*breadth;
Output:
Area of rectangle = 50
Multilevel inheritance:
class box
{
double width;
double height;
double depth;
box(double w,double h,double d)
{
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 w1)
{
super(w,h,d);
weight=w1;
}
}
class Shipment extends Boxweight
{
double cost;
Shipment(double w,double h,double d,double w1,double c)
{
super(w,h,d,w1);
cost=c;
}
}
public class pr18_1
{
public static void main(String args[])
{
Shipment s1=new Shipment(1,2,4,5,3.4);
Shipment s2=new Shipment(2,4,6,8,2.6);
double vol;
vol=s1.volume();
System.out.println("The volume of Shipment 1 is "+vol);
System.out.println("The weight of Shipment 1 is "+s1.weight);
System.out.println("The cost of Shipment 1 "+s1.cost);
double vol1;
vol1=s2.volume();
System.out.println("The volume of Shipment 2 is "+vol1);
System.out.println("The weight of Shipment 2 is "+s2.weight);
System.out.println("The cost of Shipment 2 is "+s2.cost);
}
}
Output:
The volume of Shipment 1 is 8.0