aspose 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
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: 4392
    
    8

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