Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Jaxson Brown   on Aug 12 In Java Category.

  
Question Answered By: Luigi Fischer   on Aug 12

No. Overloaded methods (i.e. selected at compile time) are not polymorphism.
They are simply overloaded methods, but highly useful for all that.

Polymorphism is the late-binding (i.e. at run-time) technique that allows
pointers to child classes of a parent to be kept in a parent class pointer and
for methods defined at the parent level and overridden at the child level to be
called correctly through the parent-level pointer.

The normal use for this is to have an array of related objects and to have the
ability to call common methods at the array level without needing to know the
precise class that the individual objects are members of.

A common example is shapes which can calculate their volume. E.g. "Cube",
"RoundBall", "OvalBall", "Funny19SidedThing" are all shapes (and are therefore
descendents of the "Shape" parent). "Shape" has a "volume" method, but of
course wouldn't have a clue how to calculate a volume. Each child of "Shape"
also has a volume method, which returns the volume of that type of shape.

If you have a collection of RoundBall's, you can simply ask each of the objects
what their volume is - calling the RoundBall.volume method directly.

If, however, you have a collection of Shape's, with items of all the different
types in it, you need to find out which type they are so you can call the
correct "volume" method. Or you can let Java do it for you.

Simply call Shape.volume (i.e. the "volume" method that is visible from the
Shape's collection) and the Java run-time will pass control to the appropriate
"volume" method for you.

The other thing that's great about this is that you can later add
"DonutShapedThing" to your family of Shape's and have it able to live in a
Shape's array and be questioned about its volume, without any changes required
to Shape or any of the other classes.

That's polymorphism. I'm not aware that any of the other techniques are
polymorphic - just useful.

Share: 

 

This Question has 23 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Polymorphism Or get search suggestion and latest updates.

Related Topics:

Tagged: