Somebody asked a question about polymorphism and another guy
gave a good example with animals, cats, and dogs that make noises :-)
Let me try another example that may enlighten you even more in the power
of polymorphism when used with Java.
Joe Smith was a programmer who wrote the following class and interface
====================================================
/** This interface states that any class that implements it, must also
implement a method called void roll() which semantycs indicates that
the given SphericalObject must roll in the way its appropriate for
the object
*/
interface SphericalObject
{ void roll();
}
====================================================
/** A LittleKid is a HumanBeing who likes to play with SphericalObject's
*/
class LittleKid extends HumanBeing
{ //.... blah blah blah other methods and stuff...
void play(SphericalObject so)
{ so.roll(); // make the so given to me roll!
System.out.println("YOOPEEEEEEE!!!!!!!!!");
}
}
====================================================
Now the poor programmer Joe Smith compiled the above, getting
SphericalObject.class and LittleKid.class with the corresponding
javadocs explaining the way the API works.
Unfortunately Joe was hit buy a truck and died and evenmore, a virus
deleted the source code, leaving only the class files and javadoc!!!!!
Six months lager the employer hires another programmer called Susan.
She writes the following classes:
class Orange extends Fruit implements SphericalObject
class Basketball extends Toy implements SphericalObject
class Globe extends Map implements SphericalObject
And then writes the following code:
..
LittleKid billy= new LittleKid();
Orange or= new Orange();
Basketball ball= new BasketBall();
Globe planetEarth= new Globe();
// Billy is bored and wants to play.
// Let's give him some SphericalObjects!!
billy.play(or);
billy.play(ball);
billy.play(planetEarth);
..
=====================================
Some observations:
Not all Fruits are SphericalObjects, nor are all kinds of Maps.
Joe Smith didn't have the time to design any actual SphericalObject
but was smart enough to design an interface.
Neither did he have to make a play method that accepts a Fruit, another
that accepts a Toy, yet another that accepts a Map. That would have been
too restrictive and in order to make LittleKid play with any new kind of
SphericalObject we would have to change the source code of LittleKid.
S**T!! It was deleted by a virus!!!
The way we did it, LittleKid and SphericalObject are written and compiled
months ni advance and can be used in ways and with objects that Joe never
dreamed of. They just have to implements the interface and provide a roll()
method. Billy will play with the SphericalObject given to him; he knows
it WILL roll (in whichever way the particular object accomplish the task).
Evenmore the compiler MAKES SURE it WILL roll because it forces the class
that implements SphericalObject to have a void roll() method, or on the
contrary, declare that the class is abstract which means it can't be
instantiated so we don't have the danger of having a SphericalObject
for which we have forgotten to provide a void roll() method.
That would be very dangerous, for if Billy was handed such an object
and he called roll()... what would then be executed?!?!? Probably
a program crash would occur.
I hope you start to fill the beauty now and everything is a little clearer!
Barzilai Spinak
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm