Java Reference
In-Depth Information
Creating an Abstract Class
As indicated in the requirements document, an object of type
PasswordException never should be created; only objects of the subclasses
should be created. To ensure this, the PasswordException class should be defined
as an abstract class, because an abstract class cannot be used to instantiate an
object, and abstract classes are used only as a basis for inheritance.
Figure 10-6 displays the code for the abstract class, PasswordException. Lines
1 through 7 add the block comment. Line 9 declares the PasswordException class
as an abstract class using the keyword, abstract. As indicated by the keyword,
extends, the PasswordException class extends the Exception class and thus,
inherits its attributes and behaviors.
The PasswordException class has two constructors. The first constructor in
lines 11 through 14 has no parameters and, therefore, is the default constructor.
The second constructor in lines 16 through 19 has a single parameter consisting
of a String passed by the calling program. Programs that create Exception objects
typically create Exception objects with or without a corresponding informational
message; these two types of constructors, thus, are common among Exception
classes. Both constructors also call the superclass constructor (lines 13 and 18)
and pass a String argument, which is used to initialize a detail message in the
superclass, Exception. This detail message later can be accessed by a user pro-
gram. Line 21 includes the abstract method, usage(), which includes a header but
no implementation. The usage() method is used to provide common behavior
for all of the PasswordException subclasses, so that when an exception is gener-
ated, each PasswordException subclass contains an additional informational
message, which also can be accessed by a user program.
FIGURE 10-6
Search WWH ::




Custom Search