• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is polymorphism

 
Biju Mahadevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Biju Mahadevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Biju Mahadevan
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Biju
I m afraid still not enough, names in Javaranch normally should consist of first name and last name.
 
greg buela
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is Overiding and overloading to be known as polymorphism.

Polymorphism apply only to overriden instance methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic