Two Laptop Bag*
The moose likes Beginning Java and the fly likes Polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polymorphism" Watch "Polymorphism" New topic
Author

Polymorphism

pradeep kurup
Greenhorn

Joined: Nov 09, 2000
Posts: 10
Hi friends,
I'm Pradeep. Recently I attended an interview in which I was asked the following question about Polymorhism. Whatever answers I gave sounded unconvincing to the interviewer.
Polymorphism as we all know is using the same name for methods with different signatures ( I mean different parameters). Now the question is :
What is the advantage of using same name for methods with different parameters whereas we can always use different names to avoid any confusion.
Can someone post me a very convincing answer to this question pl.
Thanks in anticipation
Pradeep
steve viney
Greenhorn

Joined: Nov 18, 2000
Posts: 5
Polymorphism as we all know is using the same name for methods with different signatures ( I mean different parameters). Now the question is :
I thought adding methods with different signatures was called overloading, not polymorphism !!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Polymorphism is acheived in Java through over-riding methods in a sub-class (NOT overloading methods by having different signatures).
Class Pet has method play().
Class Dog sub-classes Pet and over-rides method play() to be dog specific play.
Class Cat sub-classes Pet and over-rides method play() to be cat specific play.
Objects which are Pets can now come in "many forms" (ie. polymorphism). This allows us to use a Pet object, call it's play method, and know that the object will behave properly for it's sub-type. If the Pet is a Cat it will play the way a cat plays - without us having to know in advance that it is a cat and having to call a "catPlay" method.
Now we can do things like load up an array of Pets with Cats and Dogs, and loop through the array calling the play() method on all of the array members without knowing whether it is a cat or a dog. For this to work the methods in Cat and Dog must have the same name - and the fact that the Super class has that method with the same name tells us that the cats and dogs will be able to use a method with that name - either by overriding the method or inheriting the method.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Mohan Guru
Greenhorn

Joined: Nov 08, 2000
Posts: 8
Hi!
The question asked is related to method overloading only.
Because a method is said to be overridden only when the subclass
is defining a method of same signature(name & parameter list) &
return type as in the superclass.
The answer is:
consider finding the area of square and rectangle.
By giving the same name we can easily recogonise the purpose of the method,that is,it is used to find out the area.
Then for square one paramter is enough,for rectangle we need two
parameters.That is method overloading.It is also known as
static polymorphism.
For example you can refer java.lang.String's valueOf().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Polymorphism
 
Similar Threads
instance variables - polymorphism(overriding)
Overloading vs Polymorphism
Polymorphism
An example of polymorphism is Overloading(?) Overriding(?)
Polymorphism