• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Dout about overloaded methods

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you. I forgot that I was mixing overloaded and overriding methods.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic