• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Polymorphism

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am confused with POLYMORPHISM in overloaded and overriden methods. So, as I understood, Java doesn't give you polymorphism when you use overloading. For example,

Can't understand why it does it? Why not to use polymorphism...

Next example:

So I am confused why java sometimes uses polymorphism and sometimes not?

Thank you I appreciate your help.


Edit by mw: Added Code Tags.

[ June 06, 2008: Message edited by: marc weber ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mukhan,

It looks like you are confusing overloading and overriding. Remember the main rules:

OVERRIDING - Must have same argument list and return type (or covariant return)

OVERLOADING - Reusing a method name but with different arguments which may have different return types if argument lists are also different

So now that we've established that, let's take another look at your first example:



What do you think that is? It's on OVERLOAD, same name and return type BUT different parameters.

So, in this case you are calling eat() twice, once with an Animal and once with a Horse BUT they are BOTH an Animal Reference (the stuff on the left of the assignment) and the rule for overloading is the the reference type determines what gets called... so if you actually try to run your code you'll see that it prints "Animal is eating!!" TWICE!! where did you get your result from?

Now the second example is an OVERRIDE because you define the eat() method in Animal and then Horse OVERRIDES this method (same arguments and return type) and the rules of overriding are determined by the Object type (the right side of the assignment) so h.eat() will print "Horse is eating" BUT eat(string) is an OVERLOAD and it is determined by the reference type, which in your case is Animal... and since Animal doesn't have that overload the compiler will complain. If you change:

to

your code will compile.

Hope that helps
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See How my dog learned polymorphism.

(And please use Code Tags when posting code.)
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Noam.Nice Explanation.
 
mukhan myrzakulov
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks man, great explanation! I appreciate your help!
 
The moustache of a titan! The ad of a flea:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic