Java Reference
In-Depth Information
When inheriting from another class, a constructor, as its first statement,
must call a constructor of its immediate parent, or superclass, without creating
another instance of the superclass. A superclass default constructor can be called
implicitly and so does not require an explicit call. An explicit call, if needed, is
done by using the keyword, super . Although the JFrame class has a default con-
structor, classes extending JFrame can explicitly call a JFrame constructor and
pass a string to be set as the title bar caption. Because a class can have only one
parent, the keyword, super, always refers to the immediate superclass.
The super Keyword
Programmers use the super keyword to substitute for a class
name and to invoke an overridden base class method in a
derived class.
The DVD() Constructor Method
Figure 7-14 displays the code for the DVD() constructor method. Line 29
uses the keyword, super, to create an instance of the DVD frame and assign it the
title, Classics on DVD.
26
//construct instance of DVD
27
public DVD ()
28
{
29
super ( "Classics on DVD" ) ;
30
}
31
FIGURE 7-14
The following step inserts the constructor method code into the Classics on
DVD program.
To Code the DVD() Constructor Method
1. Enter the code from Figure 7-14.
TextPad displays the constructor method code (Figure 7-15).
constructor method
superclass
reference
FIGURE 7-15
 
Search WWH ::




Custom Search