You are here: Home / Topics / Multilevel inheritance example in Java

Multilevel inheritance example in Java

Filed under: Java on 2023-09-17 06:56:32

//  Program to explain Multilevel Inheritance.

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;
}
}

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;
}

BoxWeight() 
{
 super();
 weight = -1;
}

BoxWeight(double len, double m) 
{
 super(len);
 weight = m;
}
}

class BoxCost extends BoxWeight 
{
double cost;

BoxCost(BoxCost ob) 

 super(ob);
 cost = ob.cost;
}

BoxCost(double w, double h, double d, double m, double c) 
{
 super(w, h, d, m); 
 cost = c;
}

BoxCost() 
{
 super();
 cost = -1;
}

BoxCost(double len, double m, double c) 
{
 super(len, m);
 cost = c;
}
}

class  InheritanceMultilevelBox 
{
public static void main(String args[ ]) 
{
 BoxCost b1 = new BoxCost(10, 15, 20, 25.75, 50.5);
 BoxCost b2 = new BoxCost(2, 3, 4, 5.6, 10.5);
 double vol;

 vol = b1.volume();
 System.out.println("Volume of b1 is " + vol);
 System.out.println("Weight of b1 is " + b1.weight);
 System.out.println("Cost: Rs. " + b1.cost);
 System.out.println();

 vol = b2.volume();
 System.out.println("Volume of b2 is " + vol);
 System.out.println("Weight of b2 is " + b2.weight);
 System.out.println("Cost: Rs. " + b2.cost);
 System.out.println();
}
}


Output:

Volume of b1 is 3000.0
Weight of b1 is 25.75
Cost: Rs. 50.5

Volume of b2 is 24.0
Weight of b2 is 5.6
Cost: Rs. 10.5

About Author:
S
Shyam Dubey     View Profile
If you are good in any field. Just share your knowledge with others.