<< Chapter < Page Chapter >> Page >

The keyword super is used twice in the program in Listing 4 .

Call a parameterized constructor

The first usage of the keyword super appears as the first executable statement in the noarg constructor for the class named Super3 . This statement reads as follows:

super(500);

This statement causes the parameterized constructor for the immediate superclass (the class named SuperClass ) of the class named Super3 , to be executed before the remaining code in the constructor for Super3 is executed.

This is the mechanism by which you can cause a parameterized constructor in the immediate superclass to be executed.

What if you don't do this?

If you don't do this, an attempt will always be made to call a noarg constructor on the superclass before executing the remaining code in theconstructor for your class.

(That is why you should almost always make certain that the classes that you define have a noarg constructor in addition toany parameterized constructors that you may define.)

First executable statement in constructor

When super(parameters) is used to call the superclass constructor, it must always be the first executable statement in the constructor.

Whenever you call the constructor of a class to instantiate an object, if your constructor doesn't have a call to super as the first executable statement in the constructor, the call to the noarg constructor in the superclass is made automatically.

In other words, in order to construct an object of a class, it is necessary to first construct that part of the object attributable to the superclass. Thatnormally happens automatically, making use of the superclass constructor that doesn't take any parameters.

Calling a parameterized constructor

If you want to use a version of the superclass constructor that takes parameters, you can make your own call to super(parameters) as the first executable statement in your constructor (as was done in this program).

Accessing a superclass member variable

The second use of the super keyword in the program shown in Listing 4 uses the keyword to bypass an instance variable named data in the class named Super3 , to access and display the value of an instance variable named data in the superclass named SuperClass .

Note that in that same section of code, the this keyword is used to bypass a local variable named data in order to display the value of an instance variable named data in the class named Super3 .

Similarly, a statement without the use of either this or super is used to display the value of a local variable named data .

To disambiguate

Therefore, as stated earlier, the program uses this and super to disambiguate a local variable, an instance variable of the subclass, and aninstance variable of the superclass, where all three variables have the same name.

Accessing overridden superclass method

As mentioned earlier , if your method overrides a method in its superclass, you can use the keyword super to call the overridden version in the superclass, possibly completely bypassing the overridden version in thesubclass.

The program named Super4

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask