26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 9 Object-Oriented <strong>Program</strong>ming 473<br />

a program that need modification are those parts that require direct knowledge of the particular<br />

class that is added <strong>to</strong> the hierarchy. We will study two substantial class hierarchies<br />

and will show how objects throughout those hierarchies are manipulated polymorphically.<br />

9.11 Type Fields and switch Statements<br />

One means of dealing with objects of many different types is <strong>to</strong> use a switch statement<br />

<strong>to</strong> take an appropriate action on each object, based on that object’s type. For example, in a<br />

hierarchy of shapes in which each shape has a shapeType instance variable, a switch<br />

structure could determine which print method <strong>to</strong> call based on the object’s shapeType.<br />

There are many problems with using switch logic. The programmer might forget <strong>to</strong><br />

make such a type test when one is warranted. The programmer might forget <strong>to</strong> test all possible<br />

cases in a switch. If a switch-based system is modified by adding new types, the<br />

programmer might forget <strong>to</strong> insert the new cases in existing switch statements. Every<br />

addition or deletion of a class demands that every switch statement in the system be modified;<br />

tracking these down can be time consuming and error prone.<br />

As we will see, polymorphic programming can eliminate the need for switch logic.<br />

The programmer can use <strong>Java</strong>’s polymorphism mechanism <strong>to</strong> perform the equivalent logic<br />

au<strong>to</strong>matically, thus avoiding the kinds of errors typically associated with switch logic.<br />

Testing and Debugging Tip 9.2<br />

An interesting consequence of using polymorphism is that programs take on a simplified appearance.<br />

They contain less branching logic in favor of simpler sequential code. This simplification<br />

facilitates testing, debugging, and program maintenance. 9.2<br />

9.12 Dynamic Method Binding<br />

Suppose a set of shape classes such as Circle, Triangle, Rectangle, Square, are<br />

all derived from superclass Shape. In object-oriented programming, each of these classes<br />

might be endowed with the ability <strong>to</strong> draw itself. Each class has its own draw method, and<br />

the draw method implementation for each shape is quite different. When drawing a shape,<br />

whatever that shape may be, it would be nice <strong>to</strong> be able <strong>to</strong> treat all these shapes generically<br />

as objects of the superclass Shape. Then, <strong>to</strong> draw any shape, we could simply call method<br />

draw of superclass Shape and let the program determine dynamically (i.e., at execution<br />

time) which subclass draw method <strong>to</strong> use from the actual object’s type.<br />

To enable this kind of behavior, we declare draw in the superclass, and then we override<br />

draw in each of the subclasses <strong>to</strong> draw the appropriate shape.<br />

Software Engineering Observation 9.16<br />

When a subclass chooses not <strong>to</strong> redefine a method, the subclass simply inherits its immediate<br />

superclass’s method definition. 9.16<br />

If we use a superclass reference <strong>to</strong> refer <strong>to</strong> a subclass object and invoke the draw<br />

method, the program will choose the correct subclass’s draw method dynamically (i.e., at<br />

execution time). This is called dynamic method binding. Dynamic method binding is an<br />

important mechanism for implementing polymorphic processing of objects and will be<br />

illustrated in the case studies later in this chapter<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!