Java Reference
In-Depth Information
The
super
Reference
The reserved word
super
can be used in a class to refer to its parent
class. Using the
super
reference, we can access a parent's members.
Like the
this
reference, what the word
super
refers to depends on
the class in which it is used.
One use of the
super
reference is to invoke a parent's constructor. Let's look
at an example. Listing 9.4 shows a modification of the original
Words
program
from Listing 9.1. Similar to the original version, we use a class called
Book2
KEY CONCEPT
A parent's constructor can be
invoked using the
super
reference.
LISTING 9.4
//********************************************************************
// Words2.java Author: Lewis/Loftus
//
// Demonstrates the use of the super reference.
//********************************************************************
public class
Words2
{
//-----------------------------------------------------------------
// Instantiates a derived class and invokes its inherited and
// local methods.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
Dictionary2 webster =
new
Dictionary2 (1500, 52500);
System.out.println ("Number of pages: " + webster.getPages());
System.out.println ("Number of definitions: " +
webster.getDefinitions());
System.out.println ("Definitions per page: " +
webster.computeRatio());
}
}
OUTPUT
Number of pages: 1500
Number of definitions: 52500
Definitions per page: 35.0
Search WWH ::
Custom Search