Section 11.1. Introduction


11.1. Introduction

We now continue our study of object-oriented programming by explaining and demonstrating polymorphism with inheritance hierarchies. Polymorphism enables us to "program in the general" rather than "program in the specific." In particular, polymorphism enables us to write programs that process objects that share the same base class in a class hierarchy as simply as if they were all objects of the base class. Yet, as we send method calls in this general way, the specific objects "do the right thing."

Consider the following example of polymorphism. Suppose we create a program that simulates the movement of several types of animals for a biological study. Classes Fish, Frog and Bird represent the three types of animals under investigation. Imagine that each of these classes inherits base class Animal, which contains a method move and maintains the animal's current location as x-y coordinates. Each derived class implements method move. Our program maintains an array of references to objects of the various Animal derived classes. To simulate the animals' movements, the program sends each object the same messagemoveonce per second. However, each specific type of Animal responds to the move message in a unique waya Fish might swim two feet, a Frog might jump three feet and a Bird might fly ten feet. The program issues the same move message to each animal object generically, but each object knows how to modify its x-y coordinates appropriately for its specific type of movement. Relying on each object to know how to "do the right thing" (i.e., what is appropriate for that type of object) in response to the same method call is the key concept of polymorphism. The same message (in this case, move) sent to a variety of objects has "many forms" of resultshence the term polymorphism.

With polymorphism, we can design and implement systems that are easily extensiblenew classes can be added with little or no modification to the general portions of the program, as long as the new classes are part of the inheritance hierarchy that the program processes generically. The only parts of a program that must be altered to accommodate new classes are those that require direct knowledge of the new classes that you add to the hierarchy. For example, if we inherit from class Animal to create class Tortoise (which might respond to a move message by crawling one inch), we need to write only the Tortoise class (with a move method) and the part of the simulation that instantiates Tortoise objects. The portions of the simulation that process each Animal generically can remain the same.

This chapter has several key parts. First, we discuss common examples of polymorphism. We then provide a live-code example demonstrating polymorphic behavior. As you will soon see, you will use base class references to conveniently manipulate both base class objects and derived class objects polymorphically.

We then present a case study that revisits the employee hierarchy of Section 10.4.5. We develop a simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee's CalculateEarnings method. Though the earnings of each type of employee are calculated in a specific way, polymorphism allows us to process the employees "in the general." We add two new classes to the hierarchySalariedEmployee (for people paid a fixed weekly salary) and HourlyEmployee (for people paid an hourly salary and "time-and-a-half" for overtime). We declare a common set of functionality for all the classes in the updated hierarchy in a so-called abstract class, Employee, from which classes SalariedEmployee, HourlyEmployee and CommissionEmployee inherit directly, and from which class BasePlusCommissionEmployee inherits indirectly. As you will see, when we invoke each employee's CalculateEarnings method off a base class Employee reference, the correct earnings calculation is performed due to Visual Basic's polymorphic capabilities.

Occasionally, when performing polymorphic processing, we need to program "in the specific." Our Employee case study demonstrates that a program can determine an object's type at execution time and act on that object accordingly. The case study uses these capabilities to determine whether a particular employee object is a BasePlusCommissionEmployee, and, in that specific case, we increase the employee's base salary by 10%.

The chapter continues with an introduction to interfaces. An interface describes a set of methods that can be called on an object, but does not provide concrete implementations for the methods. Programmers can declare classes that implement (i.e., declare the methods of) one or more interfaces. Each interface method must be declared in all the classes that implement the interface. Once a class implements an interface, all objects of that class have an is-a relationship with the interface type, and all objects of the class are guaranteed to provide the functionality described by the interface. This is true of all derived classes of that class as well.

Interfaces are particularly useful for assigning common functionality to possibly unrelated classes. This allows objects of unrelated classes to be processed polymorphicallyobjects of classes that implement the same interface can respond to the same method calls. To demonstrate creating and using interfaces, we modify our payroll application to create a general accounts payable application that can calculate payments due not only for company employees, but also for invoice amounts to be billed for purchased goods. As you will see, interfaces enable polymorphic capabilities similar to those possible with inheritance.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net