File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Dout about overloaded methods

jack borton

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

Joined: Apr 06, 2010
Posts: 4543

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

Joined: Mar 06, 2009
Posts: 11

Thank you. I forgot that I was mixing overloaded and overriding methods.
I agree. Here's the link:
subject: Dout about overloaded methods
It's not a secret anymore!