The document discusses the use of the 'super()' constructor in object-oriented programming, explaining how it allows subclasses to call constructors and access members of their immediate superclass. It highlights the ability to create multilevel class hierarchies, where subclasses can inherit traits from multiple superclasses. Additionally, it provides examples of class instances and their properties, emphasizing the importance of 'super' in managing member visibility and inheritance.
The document discusses the use of the 'super()' constructor in object-oriented programming, explaining how it allows subclasses to call constructors and access members of their immediate superclass. It highlights the ability to create multilevel class hierarchies, where subclasses can inherit traits from multiple superclasses. Additionally, it provides examples of class instances and their properties, emphasizing the importance of 'super' in managing member visibility and inheritance.
Pay special attention to this constructor in BoxWeight:
Notice that super( ) is passed an object of type BoxWeight—not of
type Box. This still invokes the constructor Box(Box ob). As mentioned earlier, a superclass variable can be used to reference any object derived from that class. Thus, we are able to pass a BoxWeight object to the Box constructor. Of course, Box only has knowledge of its own members. Let’s review the key concepts behind super( ). When a subclass calls super( ), it is calling the constructor of its immediate superclass. Thus, super( ) always refers to the superclass immediately above the calling class. This is true even in a multileveled hierarchy. Also, super( ) must always be the first statement executed inside a subclass constructor.
A Second Use for super
The second form of super acts somewhat like this, except that it always refers to the superclass of the subclass in which it is used. This usage has the following general form: super.member Here, member can be either a method or an instance variable. This second form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass. Consider this simple class hierarchy: This program displays the following: i in superclass: 1 i in subclass: 2
Although the instance variable i in B hides the i in A, super allows
access to the i defined in the superclass. As you will see, super can also be used to call methods that are hidden by a subclass.
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 of B 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 called cost, which holds the cost of shipping such a parcel.