You are on page 1of 3

Roll No: 53

Practical No 18

Single inheritance:
class shape

int calculateArea(int length,int breadth)

return length*breadth;

class pr18 extends shape

public static void main(String args[])

pr18 p=new pr18();

System.out.println("Area of rectangle ="+p.calculateArea(10,5));

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

The weight of Shipment 1 is 5.0

The cost of Shipment 1 3.4

The volume of Shipment 2 is 48.0

The weight of Shipment 2 is 8.0

The cost of Shipment 2 is 2.6

You might also like