Java Reference
In-Depth Information
To Save and Compile the PasswordException Class
1.
With your Data Disk in drive A, click Compile Java on the Tools menu.
2.
If the PasswordException class contains errors, fix them in the coding
window and then recompile the program.
TextPad automatically saves and then compiles the source code for the
PasswordException class. If TextPad notifies you of compilation errors, fix
them in the PasswordException coding window and then compile again.
OTHER WAYS
1. Press
CTRL
+
1
Extending an Abstract Class
The PasswordException class exists to provide a common parent to the six
subclasses in the PasswordException class hierarchy and to enforce a common
behavior — that of providing a usage() method to provide an informational
message to a user program. As indicated by the inheritance hierarchy in Figure 10-3
on page 609, four classes inherit directly from PasswordException. Of these,
three classes — PasswordExpiredException, PasswordUsedException,
and PasswordInvalidException — can themselves be extended. The class,
PasswordinternalException, is designated as a
leaf node
, which means it cannot
have any child classes. Each child class extends the PasswordException class and
is a concrete class from which an object can be created. As previously discussed,
if a class inherits an abstract method, it must provide an implementation for that
method to be considered concrete. Each PasswordException subclass inherits the
abstract method, usage(), and thus, must implement the usage() method to be
considered concrete rather than abstract.
As indicated in the requirements document in Figure 10-2 on page 607, user
programs must be notified when the Password class generates an exception due
to an expired password or due to the entered password being in the list of
recently used values. This functionality is provided by the
PasswordExpiredException and PasswordUsedException classes, respectively.
Figures 10-8 and 10-9 display the code for the PasswordExpiredException and
PasswordUsedException classes. In the code for the PasswordExpiredException
class in Figure 10-8, lines 1 through 7 add the block comment. Line 9 declares the
PasswordExpiredException class. As indicated by the keyword, extends, the
PasswordExpiredException class extends the PasswordException class and thus,
inherits its attributes and behaviors. Lines 11 through 19 add two constructors:
the first as a default constructor (lines 11 through 14) and the second constructor
with a single parameter consisting of a String passed by the calling program (lines
16 through 19). Lines 21 through 26 implement the abstract method, usage(),
which was inherited from the parent class. When a PasswordExpiredException
exception is generated, the usage() method is available to return the concatenated
string message in lines 23 and 24.
Search WWH ::
Custom Search