Creating a Mufti level Hierarchy
Up to this point, we have been using simple class hierarchies that consist of only a super class 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 super class 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 tills type of situation occurs, each subclass inherits all of the traits found ‘in all of its super classes. In this case, C inherits all aspects of Band A. To see how a multilevel hierarchy can be useful, consider the following program. In it, the subclass Box Weight is used as a super class to create the subclass called Shipment. Shipment inherits all of the traits of Box Weight and Box, and adds a field called cost, which holds the cost of shipping such a parcel.
Because of inheritance,. Shipment can make use of the previously defined classes of
Box and Box 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 super class. The super() in Shipment calls the constructor in Box Weight. The in Box Weight calls the constructor in Box. In a class hierarchy, if a super class constructor requires parameters, then all sub classes must-pass those parameters “up the line.” This is true whether or not a subclass needs parameters of its own.In the preceding program, the entire class hierarchy, including Box, Box is shown all in one file. This isfor your convenience onfy. In all three , classes could have been placed into their own files and compiled separately. using is the norm, not the exception, ill creating class hierarchies .