A Super class Variable Can Reference a Subclass 

A reference variable of a super class can be assigned a reference to any subclass derived from that super class. You will find this aspect of inheritance quite useful in a variety of situations. For example, consider the following Here, weight box is a Terence ‘to Box Weight objects, and is a reference to Box·objects. Since Box Weight is a.subclass of Box, it is permissible to assign plain box a. reference to the weight object. important to understand that it is the type of the reference variable-not the
type of the object that It.refers’ to-that- determines what members can be accessed. That is, when a reference to a subclass object is assigned to a super class reference variable, you will have-access only to those parts of the object defined by the super class, This is why plain box.can’t access weight even when. it refers to a Box Weight object. you think about it, this makes sense, because the super class has no knowledge of what a subclass adds to it. This is why the last line of code in the preceding fragment is . commented out. It is not possible for a Box reference to access the because it does not define one.  .Although the preceding may seem a bit esoteric, it has some important practical applications-two o! which are discussed later ill this chapter.

Using Super

ht the preceding examples, classes derived from Box were. not implemented as . .•
• efficiently or as robustly as they could. have been. For example, the constructor for
Box Weight explicitly Initializes the width, height, and depth fields of Box Not only  does this duplicate code found in its super Selassie which is Inefficient but it implies that a subclass must be granted access to these members. However, there will be times · when-you will want t a supergrass that keeps the details of its Implementation . to itself (that is, that keep sits data members-private). In this case, there would be no .way for a subclass to directly access or initialize these variables on its own. Since . encapsulation is a primary attribute of  it is not surprising that Java provides a solution to this problem.  a subclass needs to refer to its’immediate super-class, it can do so by use of the keyword super super has two general . The first calls the supergrass’ constructor. The second ‘is used to access a member of the that has been hidden by a member of a subclass. Each use is examined here .

Using super to Call Super class Constructors

A subclass can call a constructor method defined by its super class by use of the following form of super:Here, Box Weight() calls super() with the parameters W,h, and d. This causes the , Box( ) constructor to be called, which initializes with, height, and depth using these values. Box Weight no longer initializes these values itself. It only needs to initialize  the value unique to it: weight. This leaves Box free to make these values private if desired  In the preceding example, super( ) was called with: three arguments. Since constructors can be overloaded, super( ) can be called using any form defined by the super class, The constructor executed will be the one that matches the arguments. For example, here is a complete implementation of Box Weight that provides constructors (or the various ways that a box can be constructed. In each case, super is called using the appropriate arguments. Notice that width, height, and depth have been”made private within Box.

Notice that super() is called with an object of type Box Weight-not of type Box. This
still invokes the constructor Box(Box ob). As mentioned earlier, a super doss variable. can be used to reference any object from that class. Thus, we are able to pa s a Box Weight 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 super class. Thus, super() always refers to the super class immediately above the calling class. is true even in a multilevel ed 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 super class of the subclass in which it is used. This usage has-the following  general

Here, member can be either a method or an instance variable
This second  of super 15 most-applicable to situations in which member names
of a subclass hide members by the same name in the super class. Consider simple class hierarchy.

[vfb id=1]

Share This