• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

no ambiguous reference error for Object?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code, if you pass a null reference to myMethod, the myMethod(String) method is called, even though myMethod(Object) also exists. If, instead of, myMethod(Object) you declare another method that takes a reference as an argument, say myMethod(Thread), you get an ambiguous reference compile error. Is there a special reason that Object arguments do not cause ambiguity here?
public class Test
{
public void myMethod(Object o)
{
System.out.println("Object");
}

public void myMethod(String s)
{
System.out.println("String");
}
public static void main(String args[])
{
Test t = new Test();
t.myMethod(null);
}
}
------------------
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Carlo,
The compiler always uses the more specific type reference. <code>Object</code> is the root class for all Java objects so it is always the least specific.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Carlo Smits
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes thanks that's the answer. I tried with Number and Float arguments and indeed it picks Float as being more specific than Number.
 
Can you smell this for me? I think this tiny ad smells like blueberry pie!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic