Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Polymorphism

 
pradeep kurup
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohan Guru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic