Polymorphism
-
Polymorphism refers to methods that can have multiple forms.
- Definition: the ability of a language to have duplicate method names in an
inheritance hierarchy (overriding) and to apply the method that is appropriate for the object
to which the method is applied (dynamic binding).
-
Polymorphism allows a number of different classes of objects to respond to the same request by
providing subclasses with methods with the same name and
signature as a method in the superclass.
- Through the use of polymorphism, one method call can cause different actions
to occur depending on the type of the object receiving the call.
-
With polymorphism, the programmer can deal in generalities and let the execution-time
environment concern itself with the specifics. The programmer can command a wide variety
of objects to behave in manners appropriate to those objects without even knowing the types
of those objects.
- Thus it is possible to write code that manipulates objects of many different
classes in a uniform and consistent manner without regard for their exact type, i.e., we can write
programs in a general fashion to handle a wide variety of existing and yet-to-be-specified related
classes.
- If we use a superclass reference to refer to a
subclass object and invoke a method, the program will choose the correct subclass's method
dynamically (i.e., at execution time). This is an example of dynamic binding.
- Suppose the developer creates a class called clsBird to capture the essense
of your basic bird.
- Our generic bird can fly (move), sing (makeNoise), eat, and build a nest.
- This describes your typical sparrow, lark, robin, chickadee, eagle, crow, or magpie. Well, the lst three
don't exactly sing, but they make some sort of noise.
- Since a robin is a typical bird, clsRobin inherits all of its attributes
and behaviors from clsBird.
- If a Robin instantiated from clsRobin and is instructed to move, the
bird will fly like your typical bird.
- But wait, not all birds fly! Some, like kiwis (the
bird, not the fruit), run rather than flying. Penguins swim (at least
most of them), and hummingbirds can both fly and hover.
- Therefor a set of bird classes such as clsPenguin,
clsKiwi, clsRobin, clsHummingbird
, etc. are all derived from superclass clsBird.
- In object-oriented programming, each of these classes inherits
the ability to move.
- Although each class has its own move method, the
move method for each bird is implemented quite differently so birds can move even
if they don't necessarily fly.
- When causing any bird to move, whatever the bird may be,
it would be nice to be able to treat all of the birds generically as objects of the superclass
clsBird and simply instruct it to move.
- Polymorphism makes it possible to do just that – to make any bird move, we simply invoke method move inherited from the
superclass clsBird and let the program determine dynamically (i.e., at execution
time) which subclass move method to use to make the bird fly, run, or swim.
-
So to make this kind of behavior possible, we declare a method (in this case, move)
in the superclass, and then we override that method in each of the subclasses that implements that behavior differently..
- In the subclass clsRobin, the inherited move
method would cause the clsRobin to fly.
- In the subclass clsHummingbird, the inherited move
method would cause the clsHummingbird to fly.
- In the subclass clsKiwi, the overridden move
method would cause the clsKiwi to run.
- In the subclass clsPenguin, the overridden move
method would cause the clsPenguin to swim.
- However, each bird is considered to be a bird that is capable of moving. That
movement is initiated by a call to the move method that is part of the
superclass clsBird interface, but the proper movement is
determined dynamically, depending on the type of object being moved.