Creating a Multilevel Hierarchy
Up to this point, we have been using simple class hierarchies that consist of only a superclass and a subclass. However, you can build hierarchies that contain as many layers of inheritance as you like. As mentioned, it is perfectly acceptable to use a subclass as a superclass of another. For example, given three classes called A, B, and C, C can be a subclass of B, which is a subclass of A. When this type of situation occurs, each subclass inherits all of the traits found in all of its superclasses. In this case, C inherits all aspects ofB and A. To see how a multilevel hierarchy can be useful, consider the following program. In it, the subclass BoxWeight is used as a superclass to create the subclass called Shipment. Shipment inherits all of the traits of BoxWeight and Box, and adds a field calledcost, which holds the cost of shipping such a parcel.
Extend BoxWeight to include shipping costs.
Start with Box.
class Box {
private double width;
private double height;
private double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
constructor used when all dimensions specified B
ox(double w, double h, double d) {
width = w; height = h; depth = d;
}
constructor used when no dimensions specified Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
constructor used when cube is created Box(double len) {
width = height = depth = len;
}
compute and return volume
double volume() {
return width * height * depth;
}
}
// Add weight.
class BoxWeight extends Box { double weight; // weight of box
// construct clone of an object
BoxWeight(BoxWeight ob) { // pass object to constructor super(ob);
weight = ob.weight;
}
constructor when all parameters are specified
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
default constructor
BoxWeight() { super(); weight = -1;
}
// constructor used when cube is created BoxWeight(double len, double m) {
super(len); weight = m;
}
}
// Add shipping costs.
class Shipment extends BoxWeight { double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor super(ob);
cost = ob.cost;
}
constructor when all parameters are specified
Shipment(double w, double h, double d,
double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
default constructor
Shipment() { super(); cost = -1;
}
// constructor used when cube is created 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, 20, 15, 10, 3.41); Shipment shipment2 =
new Shipment(2, 3, 4, 0.76, 1.28);
double vol;
vol = shipment1.volume(); System.out.println("Volume of shipment1 is " + vol); System.out.println("Weight of shipment1 is "
+ shipment1.weight); System.out.println("Shipping cost: $" + shipment1.cost); System.out.println();
vol = shipment2.volume(); System.out.println("Volume of shipment2 is " + vol); System.out.println("Weight of shipment2 is "
+ shipment2.weight); System.out.println("Shipping cost: $" + shipment2.cost);
}
}
The output of this program is shown here:
Volume of shipment1 is 3000.0
Weight of shipment1 is 10.0
Shipping cost: $3.41
Volume of shipment2 is 24.0
Weight of shipment2 is 0.76
Shipping cost: $1.28
Because of inheritance, Shipment can make use of the previously defined classes of Box and BoxWeight, adding only the extra information it needs for its own, specific application. This is part of the value of inheritance; it allows the reuse of code.
This example illustrates one other important point: super( ) always refers to the constructor in the closest superclass. The super( ) inShipment calls the constructor in BoxWeight. The super( ) in BoxWeight calls the constructor in Box. In a class hierarchy, if asuperclass constructor requires parameters, then all subclasses must pass those parameters “up the line.” This is true whether or not a subclass needs parameters of its own.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.