Java Reference
In-Depth Information
P OLYMORPHISM
Polymorphism is one of those buzzwords that sounds terrifying at first but, once un-
derstood, elicits the question, “Is that all there is?”
You have already seen that invoking a method in a particular object is some-
times referred to as a message in OO parlance. Often, more than one class can han-
dle a particular type of message. The actual function performed inside any class
depends on the algorithm of the class. Therefore, a particular message (that is, a
combination of method name and parameters) can be acted on, or interpreted, dif-
ferently by different classes. Polymorphism is defined as the ability of different
classes to support methods and properties of the same name but with different im-
plementations.
A very common example of polymorphism is the ability of most PC applica-
tions to process an open ( FileName ) message. This message will result in very dif-
ferent actions by various applications, but the message, or interface, is the same. An
originating application (such as Windows Explorer or an e-mail package) can send
this same message to any application, confident that the receiving application will
process it correctly.
Classes that are related by inheritance can each accept messages defined in the
superclass and are, therefore, polymorphic. Take another look at the previous ex-
amples, in which the objects myErrorMsg , myPopupMsg , myPrintfile , and myAnyMsg
could all receive the same message ( setErrorMsg (Any Text) ). Some of the objects
were of the same class, so it would be expected that they could handle this message.
Others were of different classes, yet they can all receive this message and process it
correctly. This is an example of polymorphism.
Unrelated classes can also be polymorphic—that is, two unrelated classes can
potentially accept the same message and respond in unique ways to it.
Suppose the system had a class called Diagnostic , and its purpose was to record
system events for the purposes of on-site diagnostics. This class may also have a
method called setErrorMsg that accepted a text String parameter. An object of this
type could receive the same message ( setErrorMsg (Any Text) ) as the ErrorMsg class
did. Java interfaces are a great way to define polymorphic behavior.
Polymorphism delivers a number of advantages to the developer. A program
can send a particular message to a whole group of classes without considering the
differences between those classes. This has the potential for greatly simplifying code,
since distinct objects can be treated in some cases as if they are of the same type. In
Java, classes that are polymorphic should be either within the same inheritance hi-
erarchy or implement the same interface. An example might be a component-based
system that consisted of many transactional objects of various types (customers, in-
voices, etc.). When the system needed each of the objects to commit their data to the
database, a single message ( commit() ) could be sent to all of the objects.
Search WWH ::




Custom Search