• 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

Overloading doubt

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I am in doubt that if i write the above program, i don't get any ambiguity error but when i add this method into it i get one. Please explain.

 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the following code

If you uncomment the third ambiguity method, then there will be an ambiguity error. As the code stands, you won't get any error because null can be passed to Object and Integer but since Integer is more specific (as its a sub-class of Object), thus the method which takes Integer is chosen. If you uncomment the third ambiguity method, then compiler is not able to choose which method should be called. Integer and String are both in different hierarchies. The same thing happens in your code too. When you don't pass any parameters, if there are 2 methods which take Object... and Integer..., then the one with Integer... is chosen. But if you introduce the third method, then the compiler is not able to choose between String... and Integer...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic