Java Reference
In-Depth Information
ple, subclasses inherit the notion of what it means to be a
Shape
—perhaps common attri-
butes such as
location
,
color
and
borderThickness
, and behaviors such as
draw
,
move
,
resize
and
changeColor
. Classes that can be used to instantiate objects are called
concrete
classes
. Such classes provide implementations of
every
method they declare (some of the
implementations can be inherited). For example, we could derive concrete classes
Circle
,
Square
and
Triangle
from abstract superclass
TwoDimensionalShape
. Similarly, we could
derive concrete classes
Sphere
,
Cube
and
Tetrahedron
from abstract superclass
ThreeDi-
mensionalShape
. Abstract superclasses are
too general
to create real objects—they specify
only what is common among subclasses. We need to be more
specific
before we can create
objects. For example, if you send the
draw
message to abstract class
TwoDimensionalShape
,
the class knows that two-dimensional shapes should be
drawable
, but it does not know
what
specific
shape to draw, so it cannot implement a real
draw
method. Concrete classes
provide the specifics that make it reasonable to instantiate objects.
Not all hierarchies contain abstract classes. However, you'll often write client code
that uses only abstract superclass types to reduce the client code's dependencies on a range
of subclass types. For example, you can write a method with a parameter of an abstract
superclass type. When called, such a method can receive an object of
any
concrete class that
directly or indirectly extends the superclass specified as the parameter's type.
Abstract classes sometimes constitute several levels of a hierarchy. For example, the
Shape
hierarchy of Fig. 9.3 begins with abstract class
Shape
. On the next level of the hier-
archy are
abstract
classes
TwoDimensionalShape
and
ThreeDimensionalShape
. The next
level of the hierarchy declares
concrete
classes for
TwoDimensionalShape
s (
Circle
,
Square
and
Triangle
) and for
ThreeDimensionalShape
s (
Sphere
,
Cube
and
Tetrahedron
).
Declaring an Abstract Class and Abstract Methods
You make a class abstract by declaring it with keyword
abstract
. An abstract class nor-
mally contains one or more
abstract methods
. An abstract method is an
instance method
with keyword
abstract
in its declaration, as in
public abstract void
draw();
// abstract method
Abstract methods do
not
provide implementations. A class that contains
any
abstract
methods must be explicitly declared
abstract
even if that class contains some concrete
(nonabstract) methods. Each concrete subclass of an abstract superclass also must provide
concrete implementations of each of the superclass's abstract methods. Constructors and
static
methods cannot be declared
abstract
. Constructors are
not
inherited, so an
abstract
constructor could never be implemented. Though non-
private
static
methods are inherited, they cannot be overridden. Since
abstract
methods are meant to
be overridden so that they can process objects based on their types, it would not make
sense to declare a
static
method as
abstract
.
Software Engineering Observation 10.4
An abstract class declares common attributes and behaviors (both abstract and concrete)
of the various classes in a class hierarchy. An abstract class typically contains one or more
abstract methods that subclasses must override if they are to be concrete. The instance
variables and concrete methods of an abstract class are subject to the normal rules of
inheritance.