Java Reference
In-Depth Information
Common Programming Error 10.1
Attempting to instantiate an object of an abstract class is a compilation error.
Common Programming Error 10.2
Failure to implement a superclass's abstract methods in a subclass is a compilation error
unless the subclass is also declared
abstract
.
Using Abstract Classes to Declare Variables
Although we cannot instantiate objects of abstract superclasses, you'll soon see that we
can
use abstract superclasses to declare variables that can hold references to objects of
any
con-
crete class
derived from
those abstract superclasses. We'll use such variables to manipulate
subclass objects
polymorphically
. You also can use abstract superclass names to invoke
static
methods declared in those abstract superclasses.
Consider another application of polymorphism. A drawing program needs to display
many shapes, including types of new shapes that you'll
add
to the system
after
writing the
drawing program. The drawing program might need to display shapes, such as
Circle
s,
Triangle
s,
Rectangle
s or others, that derive from abstract class
Shape
. The drawing pro-
gram uses
Shape
variables to manage the objects that are displayed. To draw any object in
this inheritance hierarchy, the drawing program uses a superclass
Shape
variable con-
taining a reference to the subclass object to invoke the object's
draw
method. This method
is declared
abstract
in superclass
Shape
, so each concrete subclass
must
implement
method
draw
in a manner specific to that shape—each object in the
Shape
inheritance
hierarchy
knows how to draw itself
. The drawing program does not have to worry about the
type of each object or whether the program has ever encountered objects of that type.
Layered Software Systems
Polymorphism is particularly effective for implementing so-called
layered software systems
.
In operating systems, for example, each type of physical device could operate quite differ-
ently from the others. Even so, commands to read or write data from and to devices may
have a certain uniformity. For each device, the operating system uses a piece of software
called a
device driver
to control all communication between the system and the device. The
write message sent to a device-driver object needs to be interpreted specifically in the con-
text of that driver and how it manipulates devices of a specific type. However, the write
call itself really is no different from the write to any other device in the system—place some
number of bytes from memory onto that device. An object-oriented operating system
might use an abstract superclass to provide an “interface” appropriate for all device drivers.
Then, through inheritance from that abstract superclass, subclasses are formed that all be-
have similarly. The device-driver methods are declared as abstract methods in the abstract
superclass. The implementations of these abstract methods are provided in the concrete
subclasses that correspond to the specific types of device drivers. New devices are always
being developed, often long after the operating system has been released. When you buy
a new device, it comes with a device driver provided by the device vendor. The device is
immediately operational after you connect it to your computer and install the driver. This
is another elegant example of how polymorphism makes systems
extensible
.