Java Reference
In-Depth Information
Let's consider several additional examples of polymorphism.
Quadrilaterals
If class
Rectangle
is derived from class
Quadrilateral
, then a
Rectangle
object
is a
more
specific
version of a
Quadrilateral
. Any operation (e.g., calculating the perimeter or the
area) that can be performed on a
Quadrilateral
can also be performed on a
Rectangle
.
These operations can also be performed on other
Quadrilateral
s, such as
Square
s,
Par-
allelogram
s and
Trapezoid
s. The polymorphism occurs when a program invokes a meth-
od through a superclass
Quadrilateral
variable—at execution time, the correct subclass
version of the method is called, based on the type of the reference stored in the superclass
variable. You'll see a simple code example that illustrates this process in Section 10.3.
Space Objects in a Video Game
Suppose we design a video game that manipulates objects of classes
Martian
,
Venusian
,
Plu-
tonian
,
SpaceShip
and
LaserBeam
. Imagine that each class inherits from the superclass
SpaceObject
, which contains method
draw
. Each subclass implements this method. A
screen manager maintains a collection (e.g., a
SpaceObject
array) of references to objects of
the various classes. To refresh the screen, the screen manager periodically sends each object
the
same
message—namely,
draw
. However, each object responds its
own
way, based on its
class. For example, a
Martian
object might draw itself in red with green eyes and the appro-
priate number of antennae. A
SpaceShip
object might draw itself as a bright silver flying sau-
cer. A
LaserBeam
object might draw itself as a bright red beam across the screen. Again, the
same
message (in this case,
draw
) sent to a
variety
of objects has “many forms” of results.
A screen manager might use polymorphism to facilitate adding new classes to a system
with minimal modifications to the system's code. Suppose that we want to add
Mercurian
objects to our video game. To do so, we'd build a class
Mercurian
that extends
SpaceOb-
ject
and provides its own
draw
method implementation. When
Mercurian
objects appear
in the
SpaceObject
collection, the screen-manager code
invokes method
draw
, exactly as it
does for every other object in the collection, regardless of its type.
So the new
Mercurian
objects
simply “plug right in” without any modification of the screen manager code by the pro-
grammer. Thus, without modifying the system (other than to build new classes and
modify the code that creates new objects), you can use polymorphism to conveniently
include additional types that were not even considered when the system was created.
Software Engineering Observation 10.1
Polymorphism enables you to deal in generalities and let the execution-time environment
handle the specifics. You can tell objects to behave in manners appropriate to those objects,
without knowing their specific types, as long as they belong to the same inheritance hierarchy.
Software Engineering Observation 10.2
Polymorphism promotes extensibility: Software that invokes polymorphic behavior is
independent of the object types to which messages are sent. New object types that can respond
to existing method calls can be incorporated into a system without modifying the base system.
Only client code that instantiates new objects must be modified to accommodate new types.