Java Reference
In-Depth Information
Introduction
Previously, you learned about developing a class so it can be reused by other user
programs. Developing a reusable class is useful when multiple applications, partic-
ularly unrelated applications, share a need for some common functionality.
Reusing a previously developed and thoroughly tested class to provide specific
functionality allows a programmer to be more productive by concentrating on
new aspects of the application at hand, instead of the functionality provided by the
reusable class. Using a reusable class also reduces the opportunity for errors in the
application code, because the functionality provided by the class already is tested.
Whenever you create an instance of any previously defined class in a new
application — whether from a class library or your own code — you are reusing
a class. As long as the reusable class provides exactly the functionality required,
you need only use it in your application. If you must have additional capabilities,
you still may be able to use the original class as the basis for a new subclass that
inherits the functionality of the original class and then adds to, or extends, it. As
you learned in previous chapters, this process of extending a class , which is
indicated by the keyword, extends, is the means of implementing inheritance in
Java. As discussed in Chapter 1, inheritance is the ability of one class (and, of
course, the objects instantiated from the class) to inherit attributes and behaviors
from another class. Aggregation, by contrast, is the term used to describe the
concept of an object being composed of other objects, that is, an object having
another object as an instance variable.
In this chapter, you will learn how to use inheritance to build classes that
have common functionality; how and when to use aggregation, rather than
inheritance; and how to enforce additional common functionality with interfaces
and abstract classes. You also will learn how a class can limit the overriding of
inherited methods or even prevent inheritance from taking place.
Chapter Ten — Developing a PasswordException
Class and Subclasses
In Chapter 9, a reusable Password class was developed so that AccessFour Software
could use the class to provide the same password functionality across multiple
applications. An application using that Password class, however, currently cannot
determine if a generated exception is due to expiration or to an invalid password
value. AccessFour Software now has a need for applications to be able to differenti-
ate between the various exception conditions generated by the Password class.
In this chapter, a new PasswordException class and six subclasses are created,
and the Password class developed in Chapter 9 is modified to allow for better
exception handling. The new and modified classes add functionality so that, if the
exception is due to expiration, the user is allowed to log on and change his or her
password. If the exception is due to an invalid value, the user is denied access.
The chapter also focuses on developing a stock list application to test the
PasswordException class, its subclasses, and the Password class. This test application,
the StockListDemo application, allows users to log on and build a list of stock sym-
bols. The stock list application uses multiple windows. Using multiple windows in an
application is appropriate if the application must present to a user more information
than will fit in a single window or if the application must present information or
Search WWH ::




Custom Search