Contrived Example of the Importance of Polymorphism

Nov 19, 2009 18:19

(Posted to the local Java Users Group mailing list. Reposted here for your edification, or at least hopefully enjoyment ^_^)

There are lots of reasons why polymorphism (one object being treated as a different kind of object) is a valuable thing to have in a programming language.

As a fanciful example, let's say that you are the MI6 program and you
have an object reference called "doubleOhSeven" that refers to an
instance of class Spy.

The Spy class extends (is a subclass of) the Person class.

The Spy class has an instance variable "car" that refers to an
instance of class SpyCar. So doubleOhSeven has a spy car that he can
use in his missions.

SpyCar extends (is a subclass of) the Car class, which has instance
variables like "gasPedal" and "steeringWheel" and methods like
accelerate(float rateOfAcceleration) and honk(float duration).

In addition, the SpyCar also has instance variables
"hiddenCompartment" and "rocketLauncher" and methods fireRocket(Object
target) and selfDestruct(int delayInMilliseconds)

Now, say that doubleOhSeven has been assigned a mission, and he's
trying to win the trust of beautifulWoman who is somehow connected to
the suspiciousOrganization he is supposed to infiltrate. She's
expressed an interest in his car and wants to take it for a spin. If
he lets her drive, then she'll like him and tell him her secrets (at
least that's how it always works in the movies, right?) At the same
time, he isn't sure how far he can trust her; she might be in on the
organization's shadier side, or she might just be part of the front.
Either way, he doesn't want her firing rockets at anything.

So he gives her the car, but instead of telling her that it is a
SpyCar, he pretends that it's just a Car. He can do that because it
has all of the things that a Car has and behaves just the way that
beautifulWoman (who can use Cars) would expect a Car to. She drives
it and doesn't suspect anything, has a great time, gives the car back
to him and spills her secrets to him. He later successfully
infiltrates the organization, and sufficiently damages them to put
them out of business, but then something goes wrong. His cover is
blown, he's getting shot at, and his enemy will follow him to the ends
of the earth to see him destroyed. He jumps into the car to buy some
time because its materials are all BulletProofed subclasses of
standard Car components. In addition, since he knows it's a SpyCar,
he can use the SpyCar specific methods. He calls
car.selfDestruct(90000), then car.fireRocket( badGuys ) (badGuys is a
List) to create a distraction. While they're milling about, he sneaks
out of the car and dives behind a convenient stack of crates just as
the car detonates. None of the badGuys saw him get out of the car, so
they report back to their ringLeader that doubleOhSeven was garbage
collected along with the car, and ringLeader calls off the search.

story, programming, fun, java, educational, amusing, code, awesome, polymorphism, code story

Previous post Next post
Up