Polymorphism
Polymorphism is a feature of
object-oriented systems that provides great flexibility and consistency. Polymorphism
refers to situations where the same message is sent to objects from different classes, and
different methods are executed. For example, any 2-dimension shape such as a circle or
square have an area, but the way they are calculated differs from shape to shape.
Building on the concept of a programmer may
wish to use a set of classes designed and implemented as programs by someone else, but
they may not wish to have to worry about the complexity of the implementation of each
class. If a programmer were writing a 2D drawing program they might wish to use a library
of pre-written classes for shapes, each of which provides a "getArea()" method.
The programmer can then happily send a "getArea()" message to whichever graphic
object he or she is wanting to work with, secure in the knowledge that whichever object is
chosen it will return a calculation of the area of the shape.
In the following sample of a Java program,
both lines look very similar and both objects are being sent the same message.
areaOfShape1
= shape1.getArea();
areaOfShape2 = shape2.getArea();
However, the actual methods executed
might be different, if shape1 is based on a Circle class and shape2 on a Square class.
Looking at out example for LibraryItem classes, it might be that the charge for late returns is not simply an
attribute set and stored for each individual object, but set according to the sub-class of
the object. So it might be that objects of subclass TextItem always have a
late return charge of 0.05 pounds per day, while objects of subclass Recording have a late
return charge of 50% of their weekly loan fee. In such a situation sending an LibraryItem object the message getLateReturnCharge() would result in polymorphism, since depending on which subclass the object
was an instance of, different methods would be executed for this same message.
Another example:
For example, to calculate the monthly
pay for a lecturer, one might execute a method to divide the annual salary by 12; however,
for a 0.5 part time lecturer one would need to further reduce the 12 of annual salary by
half again (or an appropriate amount for a 0.1 or 0.75 part time lecturer). In both cases
the classes would have an operation called something like caclulateMonthlyPay(), but the actual methods implemented for the Lecturer class, and the PartTimeLecturer subclass would be different.
An important advantage of polymorphism is
that it is possible to design systems in such a way that it becomes easy for adding to a
system at a later date. For example:
-
the addition of new classes (they just need to have the same
protocol as other classes, but can implement their methods differently)
-
methods can be re-implemented more efficiently at a later
date for special, with no changes to the rest of the software system.
Polymorphism is made possible through the
dynamic
and late binding of methods to objects when a program is executing (rather than at
compile time).
Back
to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|