File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloaded question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloaded question" Watch "Overloaded question" New topic
Author

Overloaded question

robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Ranchers,
could someone please help me understand why the following is not doing what i think it should be doing.



output is
Horse eat
Horse eat
Animal eat - overloaded
Horse eat - overloaded


Question:
why doesn't "a.eat(new Horse()); " reference the Horse.eat(Horse) method in a polymorphic way ? in the same way as a.eat() references the
Horse.eat() method polymorphically ? I was expecting to see the output "Horse eat - overloaded"
[ June 10, 2008: Message edited by: robert stannard ]

SCJP 1.5
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
Ask yourself, at the time of the call, what is "a" referencing? What does it point to? Has it changed since it was declared? Or does it still point to the same object?
[ June 10, 2008: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Bob,
Thanks for your question. I believe that the "a" is still pointing to the "horse" reference from the initial assignment, so when you invoke "a.eat()" you get the "horse eat" message and I was expecting the same to happen for the overloaded call a.eat(Horse). I can't see why this isn't happening. Can you?
Regards
Rob.
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
I think it's because eat(Animal) is overloaded, the method called is decided at compile time. The eat(Horse) in Horse class is just an override of the already overloaded method and not a new overloaded method (I think). What I do know is because "a" reference type is Animal, it's method in Animal class which is invoked.

You can see this if you add a new method to the Horse class which takes a different arguments and try and call it using the "a" reference. You won't be able to because its referring to the Animal class.

Hope I'm making sense, its kinda late!!!


SCJP 5.0 - Passed
Mustafa Musaji
Ranch Hand

Joined: May 03, 2008
Posts: 52
If you have K&B book check out page 108-109.
robert stannard
Ranch Hand

Joined: Jun 02, 2008
Posts: 37
Hi Mustafa,

Thanks for your comment, I read those pages in K&B you mentioned and it seems like the "rule" is that polymorphism applies only to method overriding and not to overloading.

The method invoked in an overloaded method is determinded by the reference type whereas if the method is overriden then the Object type determines which method is called.

I think I understand it now. Thanks again for your help.

Regards
Robert.
Madhawi Maurya
Ranch Hand

Joined: Aug 22, 2007
Posts: 42
In your code Animal has two methods eat() and eat(Animal), Horse has two methods eat() and eat(Horse). Here Horse overring only one method eat(). eat(Animal) and eat(Horse) methods are overloading methods not overriding methods. So reference of Animal class 'a' can refer only eat(Animal).


SCJP1.5 with 90%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloaded question
 
Similar Threads
Polymorphism
static or dynamic ..why
Polymorphism in Overloaded and Overridden Methods
Overloaded method question
overload and override method question