Java Reference
In-Depth Information
3. Save and compile the PasswordInvalidException class.
TextPad displays code for the accessor method, getCount(), and the mutator
method, resetCount(), for the class variable, count (Figure 10-14). The use
of the keyword, final, indicates that these methods cannot be overridden by
a subclass.
final keyword
mutator method
final keyword
accessor method
FIGURE 10-14
Extending a Child Class
As indicated in the requirements document in Figure 10-2 on page 607,
programs must be notified when the Password class generates an exception due
to an invalid password format. The inheritance hierarchy in Figure 10-3 on
page 609 shows the PasswordInvalidFormatException class as a child of the
PasswordInvalidException class. As such, the only required elements of the
PasswordInvalidFormatException class are its constructors.
Figure 10-15 displays the code for the PasswordInvalidFormatException class.
Line 9 declares the class and uses the keyword, extends, to indicate that the
PasswordInvalidFormatException class inherits from the PasswordInvalidException
class. Lines 11 through 19 include two constructors, and lines 21 through 24 include
the usage() method. Because the usage() method was implemented in the parent
class, the child class is not required to implement the usage() method. Line 21,
however, overrides the usage() method so that the program can return a
different String message than the one used in the parent class.
 
Search WWH ::




Custom Search