• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Ambiguity Error.

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


this code show compilation error(ambiguity), this is absolutely fine.

But, the following code doesn't show any error while compiling(How java can allow this, any one to justify this).




Thanks,
Bennet.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compile will always use the most specific arguments. Since String is more precise that Object that method is chosen.
 
Bennet Xavier
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you say, that "most specific arguments" for a null.

If you say this, it must work for other code also.

This is what i thought, but its not actual reason i suppose.

Thanks,
Bennet
 
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With String and Object, you can say that String is more specific than Object because it is a subclass of Object.

How do you decide which is more specific between String and A ? You can't and neither can the compiler and so it complains.
 
Marshal
Posts: 81619
593
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a lot about "overloading" in the Java™ Language Specification; read that section and go to the index and look up overloading, and see what you can find.
 
Ranch Hand
Posts: 63
Spring Java Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
This is the "Automatic promotion" concept of OOPS. Here String and the class A both are at same level. that means both are extending the Object class.that is why you got error in first program. Coming to the second program, you used String and Object class references as parameters. here the compiler always check for String first, If it is not matched then it will go for Object class reference. I hope you got my point.
 
I am mighty! And this is a mighty small ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic