Java Reference
In-Depth Information
new Java files
displayed
class inherits from
PasswordException
class
implements inherited
abstract method
String message returned
by PasswordUsedException
usage() method
FIGURE 10-11
As shown in the previous steps, extending an abstract class is no different than
extending any other class, such as Applet, except that you must implement any
abstract methods the abstract class may declare. Both the PasswordInvalidException
class and the PasswordUsedException class, thus, implement the abstract method,
usage(), which is inherited from the abstract class, PasswordException .
Using a Final Method in a 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 — that is, when an entered password does not match the
value of the current password. As shown in Figure 10-3 on page 609, the
PasswordInvalidException class also inherits from the parent class,
PasswordException. The PasswordInvalidException class, however, has
some additional components that the previous two classes —
PasswordExpiredException and PasswordUsedException — did not
have. Figure 10-12 on the next page displays the code for the
PasswordInvalidException class. Line 9 declares the PasswordInvalidException
class; the keyword, extends, indicates that it extends the PasswordException class.
Line 11 declares the class variable, count, with an int data type. According to
the requirements document in Figure 10-2, a count of the number of invalid
passwords used should be kept. Because an invalid password triggers a
PasswordInvalidException, a variable in the PasswordInvalidException class is an
appropriate place to increment such a count. That is, when a user enters an invalid
password, this exception is generated, and the called constructor, of the two con-
structors in lines 13 through 23, increments the count by one. The user program
then can continue and give the user another opportunity to enter the correct pass-
word. If the user enters another invalid password, a new PasswordInvalidException
object is generated. The previous PasswordInvalidException object no longer is in
 
Search WWH ::




Custom Search