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

Doubt in Overloading

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

void meth(sample s){
System.out.println("In Sample");
}

void meth(Object o){
System.out.println("In object");
}
void meth(String s){
System.out.println("In String");
}

}
public class test{
public static void main(String args[]){
new sample().meth(null);
}
}

Hi Friends,
The above program fails to compile and the compiler throws the following message.

"test.java:17: reference to meth is ambiguous, both method meth(sample) in sample
and method meth(java.lang.String) in sample match
new sample().meth(null);
^
1 error"

But if I declare a reference variable sample s=null or String s=null, the program complies.Can any one explain how this works?

regards,
Raja
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajaraman,
The following statement in your main method creates ambiguity
new Sample().meth(null);

This is because null is not an instance of any class, the compiler cannot resolve which overloaded method to call to at compile time.

You can check this with the instanceof operator
null instanceof Sample // false
null instaneof Object //false
null instaceof String //false
In other words, null is not an instanceof any class. But null can be casted to any class you like.
That is the reason you get a NullPointerException with any object and can choose to return null from any method returning an object.

If you modify the above statement as any of the following:
new Sample().meth((Sample)null); //In Sample
new Sample().meth((Object)null); //In object
new Sample().meth((String)null); //In String
it will work.

Hope that was helpful!
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Similar discussion here
 
rajaraman navaneethan
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ashish,Nitesh,
thanks for yor replies.
regards,
Raja
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic