This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is polymorphism" Watch "What is polymorphism" New topic
Author

What is polymorphism

Biju Mahadevan
Greenhorn

Joined: Sep 22, 2007
Posts: 10
Well there is a stint of confusion for me in polymorphism?. Can anyone explain me what actually is polymorphism and what is the use of it OOPS with a small example.

I read some articles, but actually i am unable to get the real value of using it or either i am lame to understand it.


Thanks in advance.


He is the best Actor in the world.<br />I am a Big Fan of <a href="http://"http://www.sivajitheboss.com"" target="_blank" rel="nofollow">Superstar RajiniKanth.</a>
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71


As you can see in this example, you can use the interface reference where you don't need to know implementation details, or where you rather not know them. The animals array may have objects of any concrete Animals. Which move() implementation executes in each iteration depends on the actual object referenced. Polymorphism lets you work through abstractions this way.
You may ask, why the hell do I need this?
I suggest to take a good look at design patterns, where you will find excellent examples of how useful and powerful this technique is.


SCJP 1.5
Biju Mahadevan
Greenhorn

Joined: Sep 22, 2007
Posts: 10
Let me phrase out on what actually i got from your reply, so that you can make me further clear whether i am wrong or not.

We create a this method and with the variable animales we execute all the methods of the classes that implements the interface.

public moveAnimals(Animal[] animals)
{
for(int i=0; i<animals.length; i++)
animals[i].move();
}

So the Output of the following program would be :

crawl
run
fly

Is that correct and actually is this polymorphism.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8875
    
    5
Hi there "the guru"

Welcome to JavaRanch! We've found that if people use their real names for their displayed names that things stay a lot frinedly - we like this idea so much that it's actually one of our few rules

So please update your display name and welcome to the ranch!

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Biju Mahadevan
Greenhorn

Joined: Sep 22, 2007
Posts: 10
Just did update my name, well i did not read the rules yet. I was just undergoing your book and the concept of polymorphism makes me slighly confused in the book.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Hi Biju
I m afraid still not enough, names in Javaranch normally should consist of first name and last name.
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
Biju: Yes, that's the output of the program, but only assuming that the array has 3 elements, in this order: a Snail, a Cheetah and an Eagle. (This wasn't implied in my code.) Whatever concrete Animal objects are in the array, their concrete move() methods will execute.
So you see that in order to use polymorphism, you need to make an object oriented design that recognizes the abstractions in the supertype. You can safely say that all your Animals will move in one way or another, so you can define a move() method at the Animal level. Then, implementation details of how specific animals have to move, go directly in the subclasses. Remember, you can have a supertype reference variable that actually points to a subclass object, i.e.:
Animal myAnimal = new Snail();
What you can do through that reference is limited by what the supertype interface exposes. But then again it's the subclass implementation that executes:
myAnimal.move(); // will display 'crawl' because the actual object is a Snail
[ September 23, 2007: Message edited by: greg buela ]
Biju Mahadevan
Greenhorn

Joined: Sep 22, 2007
Posts: 10
Slightly confused. So what does polymorphism actually implies and could anyone state the use of it. Is Overiding and overloading to be known as polymorphism.

Thank you greg buela for your explanation, but slightly confused. I tend to get more in depth in every concept that confuses me.
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424

Is Overiding and overloading to be known as polymorphism.

Polymorphism apply only to overriden instance methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is polymorphism