Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

polymorphism

 
YASIRARSALANHYD
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is polymorphism with examples now?
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really a post for the beginner-forums, but sure I'll give you a few examples.
Polymorphism is "the art of changing", that is where sub-classes performs differently then their parents. The classical examples is how animals move differently:

You then create instances of dog and fish in the code above, but reference them using animal:

Then when you call your Move()-method in the different objects, Java keeps track of which instance it is and do what's needed.
Normal implementation for this is Print()-methods. Say you have a list of different items, all you need to do is call print in each to print it. Java and poylmorphism will keep of what to print, and how.
Did that help?
/Mike
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go campfire!! great story on polymorphism.
check it out
http://www.javaranch.com/campfire/StoryPoly.jsp
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic