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 Dout about overloaded methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dout about overloaded methods" Watch "Dout about overloaded methods" New topic
Author

Dout about overloaded methods

jack borton
Greenhorn

Joined: Mar 06, 2009
Posts: 11
Hi everyone,

I was reading my book SCJP (authors: Kathy Sierra and Bert Bates) and when practicing about an exercise (page 112), there is something wrong.
I want to know if you can see if I missed something.


Second class.


Finally, the class that test the overloaded methods



The code printed is:
Generic animal eating ....
The horse is eating ....

They said that the overloaded method called is based on the reference type of the argument passed at compile time. So the answer must be twice the message "Generic animal eating .... ".
I missed something?

Thank you.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

The overloaded method called depends on the reference type. But both doStuff methods do the same thing...they call the eat() method.

And the overridden method called depends on the actual type of the object, which is why you get the result you see.

If you put print statements in the two doStuff methods, you'd see that doStuff(Animal a) is being called twice.
jack borton
Greenhorn

Joined: Mar 06, 2009
Posts: 11
Hi,

Thank you. I forgot that I was mixing overloaded and overriding methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dout about overloaded methods
 
Similar Threads
my problem related to overriding
Polymorphism help
overloaded method call rules
which version of method is actually called ? the reference one or actual object one ?
Polymorphism