Inheritance Example: Employee


Polymorphic Behavior

A good definition of polymorphism is “The ability to operate on and manipulate different derived objects in a uniform way. (Sadr) Add to this the following amplification: “Without polymorphism, the developer ends up writing code consisting of large case or switch statements. This is in fact the litmus test for polymorphism. The existence of a switch statement that selects an action based upon the type of an object is often a warning sign that the developer has failed to apply polymorphic behavior effectively.” (Booch)

Polymorphic behavior is easy to understand. In a nutshell it is simply the act of using the set of public interface methods defined for a particular class (or interface) to interact with that class’s (or interface’s) derived classes. When you write code you need some level of apriori knowledge about the type of objects your code will manipulate. In essence, you have to set the bar to some level, meaning that at some point in your code you need to make an assumption about the type of objects with which you are dealing and the behavior they manifest. An object’s type, as you know, is important because it specifies the set of operations (methods) that are valid for objects of that type (and subtypes).

Code that’s written to take advantage of polymorphic behavior is generally cleaner, easier to read, easier to maintain, and easier to extend. If you find yourself casting a lot you are not writing polymorphic code. If you use the instanceof operator frequently to determine object types then you are not writing polymorphic code. Polymorphic behavior is the essence of object-oriented programming.

Quick Review

Polymorphic behavior is achieved in a program by targeting a set of operations (methods) specified by a base class or interface and manipulating their derived class objects via those methods. This uniform treatment of derived class objects results in cleaner code that’s easier to extend and maintain. Polymorphic behavior is the essence of object-oriented programming.




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