Please, wait while we are validating your browser
Harvey M. Deitel and Paul J. Deitel, authors of Java for Programmers, explain and demonstrate the concept of polymorphism with inheritance hierarchies.This chapter is from the book
- One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them.
- —John Ronald Reuel Tolkien
- General propositions do not decide concrete cases.
- —Oliver Wendell Holmes
- A philosopher of imposing stature doesn’t think in a vacuum. Even his most abstract ideas are, to some extent, conditioned by what is or is not known in the time when he lives.
- —Alfred North Whitehead
- Why art thou cast down, O my soul?
- —Psalms 42:5
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 superclass in a class hierarchy as if they are all objects of the superclass; this can simplify programming.
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 , and represent the three types of animals under investigation. Imagine that each of these classes extends superclass , which contains a method and maintains an animal’s current location as x-y coordinates. Each subclass implements method . Our program maintains an array of references to objects of the various subclasses. To simulate the animals’ movements, the program sends each object the same message once per second—namely, . However, each specific type of responds to a message in a unique way—a might swim three feet, a might jump five feet and a might fly ten feet. The program issues the same message (i.e., ) 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., do 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, ) sent to a variety of objects has “many forms” of results—hence the term polymorphism.
With polymorphism, we can design and implement systems that are easily extensible—new 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 the programmer adds to the hierarchy. For example, if we extend class to create class (which might respond to a message by crawling one inch), we need to write only the class and the part of the simulation that instantiates a object. The portions of the simulation that process each generically can remain the same.
This chapter has several parts. First, we discuss common examples of polymorphism. We then provide an example demonstrating polymorphic behavior. We’ll use superclass references to manipulate both superclass objects and subclass objects polymorphically.
We then present a case study that revisits the employee hierarchy of Section 9.4.5. We develop a simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s 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.” In the case study, we enlarge the hierarchy to include two new classes— (for people paid a fixed weekly salary) and (for people paid an hourly salary and so-called 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, , from which classes , and inherit directly and class inherits indirectly. As you’ll soon see, when we invoke each employee’s method off a superclass reference, the correct earnings calculation is performed due to Java’s polymorphic capabilities.
Occasionally, when performing polymorphic processing, we need to program “in the specific.” Our case study demonstrates that a program can determine the type of an object at execution time and act on that object accordingly. In the case study, we use these capabilities to determine whether a particular employee object is a. If so, we increase that employee’s base salary by 10%.
Next, the chapter introduces interfaces. An interface describes methods that can be called on an object, but does not provide concrete method implementations. You can declare classes that implement (i.e., provide concrete implementations for 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 subclasses 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 polymorphically—objects 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 for company employees and invoice amounts to be billed for purchased goods. As you’ll see, interfaces enable polymorphic capabilities similar to those possible with inheritance.