This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Method with null as arg  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below code, compiler error with message 'method is ambiguous'.

If I comment the method with StringBuffer as argument it compiles and gives "Object" as output. Can somebody explain why this happen ?

 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you comment the method that takes in a String argument it would work. The reason is that null is a valid argument for Object, String and StringBuffer. But the method with closest match is chosen over other methods for execution. Now when we have methods that accept Object type and String type of argument. String is more specific so the compiler knows that String method should be used. But when you have two or more completely different things(no where related in the class hierarchy) as the argumnet it is impossible for the compiler to determine which method should be called. Hence the error.
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Anupam Sinha:
But when you have two or more completely different things(no where related in the class hierarchy) as the argumnet it is impossible for the compiler to determine which method should be called. Hence the error.



Even if the classes are related in the class hierarchy, it may be impossible for the compiler to decide which function should be called.

 
Shubha Kirani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anupam and Manfred for your replies. I'm understanding Java better now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!