Java Reference
In-Depth Information
Abstract and Concrete Classes
Like interfaces, classes also may have method headers without
implementations. A method that includes only a method header without the
implementation is called an abstract method . Because a class typically provides
the implementation for most methods, simply omitting the implementation
would cause a compiler error. To distinguish methods that intentionally do not
have an implementation, Java uses the keyword, abstract , with the method
header. In an interface, all methods are required to be abstract. Therefore, the use
of the keyword, abstract, is not required for method headers in an interface.
Whenever a class contains an abstract method, it cannot be used to instanti-
ate an object and must be declared as an abstract class. An abstract class is a class
that cannot be used to create an object and is used only for purposes of inheri-
tance, that is, to establish a base, or parent, class for future subclasses. An abstract
class is designated by use of the keyword, abstract, in the class header. In this
chapter, the PasswordException class is defined as an abstract class used to pro-
vide a parent class for the six child classes shown in Figure 10-3 on page 609.
Deciding between Abstract Classes and Interfaces
If you want to prevent instantiation of an object, but need to
define instance variables, implemented instance methods, or static
methods, then you need to use an abstract class. If you need to
enforce behavior through multiple inheritance, then you need to
use an interface.
By contrast, a concrete class is a class from which an object may be created
and may or may not be a parent class. A concrete class cannot contain any abstract
methods. If a class, which otherwise would be considered as concrete, inherits an
abstract method, it must provide an implementation for that method. If it does
not, then the class must be declared as an abstract class. When declaring a method
or a class as abstract, the method or class also must be declared as public.
PROGRAM IMPLEMENTATION Once you have designed the class and
validated the design, the next step in the development cycle is to implement the
design, as shown in Table 10-1 on page 606. In this project, several subclasses of
the PasswordException class are needed, rather than a single, comprehensive
PasswordException class. In Java, it is not uncommon to create a number of
smaller classes, especially when using inheritance. Although not strictly neces-
sary, the new PasswordException class and subclasses should be created in the
same location as the Password class. Doing so ensures that the compiler can
locate the new classes when compiling the Password class, which also will require
changes.
Further, because each subclass or child class depends on the existence of the
parent class from which it inherits, the parent and child classes must be coded
in the proper order before they can be compiled. For example, the base class,
PasswordException, must be coded first, before any of the child classes can be
coded.
Search WWH ::




Custom Search