Inheritance Concepts In Java
inheritance
process
*/
// A complete implementation of BoxWeight
/*
This program illustrates the power of inheritance
*/
class Box
{
private double width;
private double height;
private double depth;
//constructor used when no dimensions specified.
Box()
{
width = height = depth = -1;
}
//constructor used when cube is created.
Box(double side)
{
width = height = depth = side;
}
// constructor used when all dimensions specified.
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
// construct clone of an object
Box(Box obj)
{
width = obj.width;
height = obj.height;
depth = obj.depth;
}
// compute return volume
double volume ()
{
return depth * height * depth;
}
}
// here, Box is extended to include weight
class BoxWeight extends Box
{
double weight; // weight of Box
// constructor for BoxWeight class with no values initialized
BoxWeight()
{
super();
weight =-1;
}
// constructor for BoxWeight class,for creating a cube object
BoxWeight(double len, double m)
{
super(len);
weight = m;
}
// constructor for BoxWeight class, accepting four arguments (three of sides and one of weight)
BoxWeight(double w, double h, double d, double m)
{
super(w,h,d);
weight = m;
}
// constructor for BoxWeight class
BoxWeight(Box obj, double m)
{
super(obj);
weight = m;
}
}
// Implementation of class BoxWeight class
class BoxWeightDemo1
{
public static void main(String args[])
{
BoxWeight MyBox1 = new BoxWeight(23.33,33.33,43.33,50);
BoxWeight MyBox2 = new BoxWeight(33.33,43.33,53.33,150);
BoxWeight MyBox3 = new BoxWeight(MyBox1,150);
double vol = MyBox1.volume();
System.out.println(“volume of MyBox1 is : “+vol);
System.out.println(“weight of MyBox1 is : “+MyBox1.weight+” kgs”);
vol = MyBox2.volume();
System.out.println(“volume of MyBox2 is : “+vol);
System.out.println(“weight of MyBox2 is : “+MyBox2.weight+” kgs”);
vol = MyBox3.volume();
System.out.println(“volume of MyBox3 is : “+vol);
System.out.println(“weight of MyBox3 is : “+MyBox3.weight+” kgs”);
}
}
Explanation : super is called with an object of BoxWeight and not of type Box. This still invokes the constructor Box(Box obj).
lets consider another example, now consider BoxColorDemo class again,
// complete implementation of BoxColor
/* This program illustrates the power of inheritance */
class Box
{
private double width,height,depth;
//constructor used when no dimensions specified.
Box()
{
width = height = depth = -1;
}
//constructor used when all dimensions specified.
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}
//constructor used when cube is created.
Box(double side)
{
width = height = depth = side;
}
//construct clone of an object
Box(Box obj)
{
width = obj.width;
height = obj.height;
depth = obj.depth;
}
//compute return volume
double volume ()
{
return depth * height * depth;
}
}
// here, Box is extended to include color
class BoxColor extends Box
{
String color;// color of Box
// constructor for BoxColor, for a cube, class
BoxColor(double len, String str)
{
super(len);
color = str;
}
// constructor for BoxColor class
BoxColor(double w, double h, double d, String str)
{
super(w,h,d);
color =str;
}
// constructor for BoxColor class accepting object of type class as parameter
BoxColor(Box obj,String str)
{
super(obj);
color = str;
}
}
// implementation of BoxColor
class BoxColorDemo1
{
public static void main(String args[])
{
BoxColor MyBox1 = new BoxColor(23.33,33.33,43.33,”RED”);
BoxColor MyBox2 = new BoxColor(33.33,43.33,53.33,”BLUE”);
BoxColor MyBox3 = new BoxColor(MyBox1,”BLUE”);
double vol = MyBox1.volume();
System.out.println(“volume of MyBox1 is : “+vol);
System.out.println(“Color of MyBox1 is : “+MyBox1.color);
vol = MyBox2.volume();
System.out.println(“volume of MyBox2 is : “+vol);
System.out.println(“Color of MyBox2 is :