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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

regardless of the object’s type, so the new Mercurian objects just “fit right in.” Thus,<br />

with polymorphism, new types of objects not even envisioned when a system is created can<br />

be added without modifications <strong>to</strong> the system (other than the new class itself, of course).<br />

Through the use of polymorphism, one method call can cause different actions <strong>to</strong><br />

occur, depending on the type of the object receiving the call. This gives the programmer<br />

tremendous expressive capability. We will see examples of the power of polymorphism in<br />

the next several sections.<br />

Software Engineering Observation 9.19<br />

With polymorphism, the programmer can deal in generalities and let the execution-time environment<br />

concern itself with the specifics. The programmer can command a wide variety of<br />

objects <strong>to</strong> behave in manners appropriate <strong>to</strong> those objects without even knowing the types of<br />

those objects. 9.19<br />

Software Engineering Observation 9.20<br />

Polymorphism promotes extensibility: Software written <strong>to</strong> invoke polymorphic behavior is<br />

written independent of the types of the objects <strong>to</strong> which messages (i.e., method calls) are sent.<br />

Thus, new types of objects that can respond <strong>to</strong> existing messages can be added in<strong>to</strong> such a<br />

system without modifying the base system. 9.20<br />

Software Engineering Observation 9.21<br />

If a method is declared final, it cannot be overridden in subclasses, so method calls may<br />

not be sent polymorphically <strong>to</strong> objects of those subclasses. The method call may still be sent<br />

<strong>to</strong> subclasses, but they will all respond identically rather than polymorphically. 9.21<br />

Software Engineering Observation 9.22<br />

An abstract class defines a common interface for the various members of a class hierarchy.<br />

The abstract class contains methods that will be defined in the subclasses. All classes<br />

in the hierarchy can use this same interface through polymorphism. 9.22<br />

Although we cannot instantiate objects of abstract superclasses, we can declare<br />

references <strong>to</strong> abstract superclasses. Such references can be used <strong>to</strong> enable polymorphic<br />

manipulations of subclass objects when such objects are instantiated from concrete classes.<br />

Let us consider more applications of polymorphism. A screen manager needs <strong>to</strong> display<br />

a variety of objects, including new types of objects that will be added <strong>to</strong> the system<br />

even after the screen manager is written. The system may need <strong>to</strong> display various shapes<br />

(the superclass is Shape), such as Circle, Triangle and Rectangle. Each shape<br />

class is derived from superclass Shape. The screen manager uses superclass Shape references<br />

<strong>to</strong> manage the objects <strong>to</strong> be displayed. To draw any object (regardless of the level<br />

at which that object appears in the inheritance hierarchy), the screen manager uses a superclass<br />

reference <strong>to</strong> the object and simply sends a draw message <strong>to</strong> the object. Method draw<br />

has been declared abstract in superclass Shape and has been overridden in each of the<br />

subclasses. Each Shape object knows how <strong>to</strong> draw itself. The screen manager does not<br />

have <strong>to</strong> worry about what type each object is or whether the screen manager has seen<br />

objects of that type before—the screen manager simply tells each object <strong>to</strong> draw itself.<br />

Polymorphism is particularly effective for implementing layered software systems. In<br />

operating systems, for example, each type of physical device could operate quite differently<br />

from the others. Even so, commands <strong>to</strong> read or write data from and <strong>to</strong> devices can have a<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!