File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this?

 
Rishi Yagnik
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
class abc{
public void amethod(Object o){
System.out.println("Object version");
}
public void amethod(String str){
System.out.println("String version");
}

public static void main(String[] args){
abc ob=new abc();
ob.amethod(null);
}
}//abc
after compiling i am getting ans as String version can some one explain me why?
 
Rob Acraman
Ranch Hand
Posts: 89
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever there is an ambiguous function call, Java will always attempt to call the most specific class available.
In this case, since null matches both Object and String, and since String inherits from Object, String is the most specific.
Note that, if you had "sibling" methods - eg. you added "amethod(Integer x)", then there would be no clear-cut 'most specific' method so you would get an error.
 
Rishi Yagnik
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob
i got u infact i have got the same concept in my mind but thanks for Explanation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic