Java Reference
In-Depth Information
FIGURE 10-15
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 size. The current password size, as well as the maximum
and minimum sizes allowed, should be available to the exception handler. The
inheritance hierarchy in Figure 10-3 on page 609 also shows the
PasswordSizeException class as a child of the PasswordInvalidException class. As
such, it inherits attributes and behaviors from its parent classes. The reason for
extending classes typically is to incorporate additional functionality, beyond
what any of the parent classes provide. The PasswordSizeException class, for
example, needs to retain information about the size of the password entered by
the user, as well as the maximum and minimum sizes allowed, so that this
information can be passed to the user program, if desired.
Figure 10-16 on the next page displays the code for the PasswordSizeException
classes. Lines 12 through 14 declare three private instance variables to hold the
values for the size of the password entered by the user, as well as the maximum
and minimum sizes allowed. In lines 22 through 24, the keyword, this, is used to
distinguish between the three instance variables and constructor parameters of
the same name in line 19. Lines 27 through 40 include three accessor methods
to allow access to these values. There are no mutator methods, as these values are
set at the time the object is constructed. Further, because it has no default con-
structor, PasswordSizeException objects must be created passing the required
size information, in addition to a String message. Finally, because the method,
usage(), is not overloaded, the method supplied by the immediate parent class
(PasswordInvalidFormatException) will be used when called for objects of this
class.
 
Search WWH ::




Custom Search