Inheritance Concepts In Java

“+MyBox2.color);

vol = MyBox3.volume();

System.out.println(“volume of MyBox3 is  : “+vol);

System.out.println(“Color of MyBox3 is  : “+MyBox3.color);

}

}

Explanation : super is called with an object of BoxColor and not of type Box. This still invokes the constructor Box(Box obj).

Remember

ü  When a sub class calls super(), it is calling the constructor of its immediate super class.

ü  super() always refers to the super class immediately above the calling class.

ü  Also, super() must always be the first statement to be executed inside a subclass constructor.

1.1 Lets examine of super in multilevel hierarchy , the below program which reveals the  usage

/*

In this program Box is used as a super class of BoxWeight and (this subclass ) Box weight

is used as a   super class of ShipMent

*/

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, class Box is extended to include weight

class BoxWeight extends Box

{

double weight; // weight of Box

// constructor used when dimensions are not specified,for BoxWeight class

BoxWeight()

{

super();

weight = -1;

}

// construct a cube of BoxWeight class

BoxWeight(double len, double m)

{

super(len);

weight = m;

}

// constructor for BoxWeight class,when all dimensions are specified.

BoxWeight(double w, double h, double d, double m)

{

super(w,h,d);

weight = m;

}

// construct a clone, for BoxWeight class

BoxWeight(BoxWeight obj)

{

super(obj);

weight =obj.weight;

}

}

// Here, class BoxWeight is extended to include cost

class ShipMent extends BoxWeight

{

double cost;

//construct when no values are supplied

ShipMent()

{

super();

cost = -1;

}

//construct when cube create

ShipMent(double side,double m,double c)

{

super(side,m);

cost = c;

}

//construct when all values supplied

ShipMent(double w,double h,double d,double m,double c)

{

super(w,h,d,m);

cost = c;

}

//construct clone of object

ShipMent(ShipMent obj)

{

super(obj);

cost = obj.cost;

}

}

class ShipMentDemo

{

public static void main(String args[])

{

ShipMent MyBox1 = new ShipMent(23.33,33.33,43.33,50,153.89);

ShipMent MyBox2 = new ShipMent(33.33,150,304.78);

ShipMent MyBox3 = new ShipMent(MyBox1);

double vol = MyBox1.volume();

System.out.println(“volume of MyBox1 is  : “+vol);

System.out.println(“weight of MyBox1 is  : “+MyBox1.weight+” kgs”);

System.out.println(“weight of MyBox1 is  : “+MyBox1.cost+”/-“);

vol = MyBox2.volume();

System.out.println(“volume of MyBox2 is  : “+vol);

System.out.println(“weight of MyBox2 is  : “+MyBox2.weight+” kgs”);

System.out.println(“weight of MyBox2 is  : “+MyBox2.cost+”/-“);

vol = MyBox3.volume();

System.out.println(“volume of MyBox3 is  : “+vol);

System.out.println(“weight of MyBox3 is  : “+MyBox3.weight+” kgs”);

System.out.println(“weight of MyBox3 is  : “+MyBox3.cost+”/-“);

}

}

Explanation :

1.2       In which order, are the constructors called?

In a class hierarchy, constructors are called in the order of derviation, that is from super class to sub class,
Further, since super() must be the first statement, to be executed in a subclass constructor. This order is the same whether of not, super() is used. If super() is not used, then the default (parameter less) for each super class will be executed constructor.

Lets  understand the above said with an example,shown below

/*

A Program demonstrating order of call, of constructors’

*/

class A

{

int i,j;

A()

{

System.out.println(“Hi The constructor of A class is

Pages: 1 2 3 4 5 6 7