Composition-Based Design As A Force Multiplier


Applied Polymorphism

Chapter 11 touched on the topic of polymorphic behavior and conceptually it is easy to grasp. Polymorphism is the ability to treat different objects in the same manner. In object oriented programming this means that your program utilizes references to base class types (preferably interfaces or abstract class types) that, at runtime, actually contain references to derived class objects.

You must plan for the proper use of polymorphic behavior from the moment you start laying the foundation of your application architecture. This means you must consider carefully your choice of inheritance forms when designing your class inheritance hierarchies.

Since the goal of polymorphic programming is the uniform treatment of derived class objects it follows that derived classes should conform to the interface specified by the base class. It also follows, then, that the preferred form of inheritance to satisfy this requirement would be functional variation, where the base class specifies behavior via an abstract method and derived classes override the method to provide a custom implementation.

Using extension inheritance instead of functional variation adds complications. For example, if a derived class extends the behavior of a base class by defining additional methods, then objects of this new type, accessed via a base class reference, must be cast to the proper type before the new functionality can be accessed.

Refer to the Person-Employee class diagram shown in figure 22-2. There are four ways to get polymorphic behavior from this inheritance hierarchy: 1) create an Object type reference and initialize it to point to either an HourlyEmployee or SalariedEmployee type object, 2) create a Person type reference and initialize it to point to either an HourlyEmployee or SalariedEmployee type object, 3) create an Employee type reference and initialize it to point to either an HourlyEmployee or SalariedEmployee type object, or 4) create a Payable type reference and initialize it to point to either an HourlyEmployee or SalariedEmployee type object. Each approach places restrictions on what functionality can be accessed via the reference without casting.

The Object reference will only allow methods defined in the Object class to be called. If a Person reference is used then Person and Object methods can be called. If a Payable reference is used then only the pay() method can be called, while the use of the Employee reference allows Person, Object, and Payable methods to be called.

Quick Review

Polymorphism is the ability to treat different objects in the same manner. In object oriented programming this means that your program utilizes references to base class types (preferably interfaces or abstract class types) that, at runtime, actually contain references to derived class objects.

You must plan for the proper use of polymorphic behavior from the moment you start laying the foundation of your application architecture. This means you must consider carefully your choice of inheritance forms when designing your class inheritance hierarchies.




Java For Artists(c) The Art, Philosophy, and Science of Object-Oriented Programming
Java For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504052
EAN: 2147483647
Year: 2007
Pages: 452

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