476 Object-Oriented Programming Chapter 9 regardless of the (Web design online)

476 Object-Oriented Programming Chapter 9 regardless of the object s type, so the new Mercurian objects just fit right in. Thus, with polymorphism, new types of objects not even envisioned when a system is created can be added without modifications to the system (other than the new class itself, of course). Through the use of polymorphism, one method call can cause different actions to occur, depending on the type of the object receiving the call. This gives the programmer tremendous expressive capability. We will see examples of the power of polymorphism in the next several sections. Software Engineering Observation 9.19 With polymorphism, the programmer can deal in generalities and let the execution-time environment concern itself with the specifics. The programmer can command a wide variety of objects to behave in manners appropriate to those objects without even knowing the types of those objects. Software Engineering Observation 9.20 Polymorphism promotes extensibility: Software written to invoke polymorphic behavior is written independent of the types of the objects to which messages (i.e., method calls) are sent. Thus, new types of objects that can respond to existing messages can be added into such a system without modifying the base system. Software Engineering Observation 9.21 If a method is declared final, it cannot be overridden in subclasses, so method calls may not be sent polymorphically to objects of those subclasses. The method call may still be sent to subclasses, but they will all respond identically rather than polymorphically. Software Engineering Observation 9.22 An abstract class defines a common interface for the various members of a class hierarchy. The abstract class contains methods that will be defined in the subclasses. All classes in the hierarchy can use this same interface through polymorphism. Although we cannot instantiate objects of abstract superclasses, we can declare references to abstract superclasses. Such references can be used to enable polymorphic manipulations of subclass objects when such objects are instantiated from concrete classes. Let us consider more applications of polymorphism. A screen manager needs to display a variety of objects, including new types of objects that will be added to the system even after the screen manager is written. The system may need to display various shapes (the superclass is Shape), such as Circle, Triangle and Rectangle. Each shape class is derived from superclass Shape. The screen manager uses superclass Shape references to manage the objects to be displayed. To draw any object (regardless of the level at which that object appears in the inheritance hierarchy), the screen manager uses a superclass reference to the object and simply sends a draw message to the object. Method draw has been declared abstract in superclass Shape and has been overridden in each of the subclasses. Each Shape object knows how to draw itself. The screen manager does not have to worry about what type each object is or whether the screen manager has seen objects of that type before the screen manager simply tells each object to draw itself. Polymorphism is particularly effective for implementing layered software systems. In operating systems, for example, each type of physical device could operate quite differently from the others. Even so, commands to read or write data from and to devices can have a Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Leave a Reply