You are on page 1of 6

Page No.

65

Ex no: MULTILEVEL INHERITANCE Date: AIM : To write a java program to implement the multilevel inheritance. ALGORITHM:

1. Start the program. 2. Assign the class name as box. 3. Constructor is created for initializing the object. 4. Parameterized constructor passes the values to the corresponding variable. 5. Default constructor allocates or initializes the value for instant variable. 6. Instance methods defined by all the multiplication.

7. Class box weight inherits the class box by using the class box by using extends by box.
8. Then it creates the parameterized constructor. 9. Super keyword represents the box and it calls the base class object for this class. 10. Parameterized constructor also calls the super class variables. 11. Default constructor also calls the super class variables. 12. Class shipment inherits the box weight. 13. Instance variable cost is declared. 14. Constructor is created then it calls the super class object. 15. Parameterized constructor also calls the super class instance variables. 16. The function volume is called by volume; it shows the volume of shipment 2.

17. Also it shows the weight of shipment 2 and shipping cost

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No.

66

PROGRAM: class Box { private double width; private double height; private double depth; Box(Box ob) { width=ob.width; height=ob.height; depth=ob.depth; } Box(double w,double h,double d) { width=w; height=h; depth=d; } Box() { width=-1; height=-1; depth=-1; } Box(double len) { width=height=depth=len; } double volume() { return width*height*depth; } }

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No.

67

class Boxweight extends Box { double weight; Boxweight(Boxweight ob) { super(ob); weight=ob.weight; } Boxweight(double w,double h,double d,double m) { super(w,h,d); weight=m; } { super(); weight=-1; } Boxweight(double len,double m) { super(len); weight=m; } } class shipment extends Boxweight { double cost; shipment(shipment ob) { super(ob); cost=ob.cost; } shipment(double w,double h,double d, double m,double c) {

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No.

68

super(w,h,d,m); cost=c; } shipment() { super(); cost=-1; } shipment(double len,double m,double c) { super(len,m); cost=c; } } class demoshipment { public static void main(String args[]) { shipment shipment1= new shipment(10.0,20.0,15.0,10.3,41.0); shipment shipment2= new shipment(20.3,41.76,1.28); shipment shipment3= new shipment(); double vol; vol=shipment1.volume(); System.out.println("Vol of shipment1:"+vol); System.out.println("Weight of shipment1:"+shipment1.weight); System.out.println("Shipping cost:$"+shipment1.cost); vol=shipment2.volume(); System.out.println("Volume of shipment 2:"+vol); System.out.println("Weight of shipment 2:"+shipment2.weight); System.out.println("Shipping cost:$"+shipment2.cost); vol=shipment3.volume(); System.out.println("Volume of shipment 3:"+vol);

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No.

69

System.out.println("Weight of shipment 3:"+shipment3.weight); System.out.println("Shipping cost:$"+shipment3.cost); } }

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

Page No.

70

RESULT: Thus the java program for multilevel inheritance has been executed and verified successfully.

VIVEKANANDHA EDUCATIONAL INSTITUTIONS

You might also like