Java Reference
In-Depth Information
The following step enters code for a concatenated method call to the
resetCount() method of the PasswordInvalidException object.
To Create a Concatenated Method Call
1. Enter line 195 as shown in Figure 10-33 on the previous page. Save and
compile the changes to the Password class.
The newly entered code displays (Figure 10-34). This line of code concate-
nates method calls to create a new PasswordInvalidException object and call
its resetCount() method, after which the object will be available for garbage
collection.
FIGURE 10-34
concatenated
method calls
The changes to the Password class now are complete. This class now uses the
PasswordException class and subclasses to handle exceptions, rather than the
more general Exception class.
Creating a Program to Test the PasswordException
Classes and Subclasses
To effectively test the Password class with the new PasswordException classes
requires a test program that uses the classes to verify the password of an existing
user. As shown in Figure 10-1 on page 605, this test application, the
StockListDemo application, allows users to log on and build a list of stock sym-
bols. In the previous chapter, the Password class was tested without respect to a
user. It is more realistic, however, to have a user associated with the password.
Creating a Class Using Aggregation
Because the test program does not use a database, a list of users will be
maintained in memory, much like the list of passwords in the previous chapter.
Each user in the list will have a name and an associated Password object. To
maintain this list of users, a simple User class is created with two attributes: a
String for name and a Password object. By incorporating the Password object,
the User class illustrates the use of aggregation. As you have learned, aggregation
is used to describe the concept of an object being composed of other objects,
that is, an object having another object as an instance variable.
Search WWH ::




Custom Search