Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why complie time error?

 
Rishi Yagnik
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
class abc{
public void amethod(String str){
System.out.println("Object version");
}
public void amethod(StringBuffer sb){
System.out.println("String version");
}
public static void main(String[] args){
abc ob=new abc();
ob.amethod(null);
}
}//abc
i am getting complie time error can some one explain me why?
Rishi
 
Rishi Yagnik
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i am sorry but my programme look like this:
class abc{
public void amethod(String str){
System.out.println("String version");
}
public void amethod(StringBuffer sb){
System.out.println("StringBuffer version");
}
public static void main(String[] args){
abc ob=new abc();
ob.amethod(null);
}
}//abc
i am getting complie time error can some one explain me why?
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob's actually answered your question in the response to your other query. He said:-
"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."
The problem here is that both methods take equally specific arguements - both String and StringBuffer extend from Object and there's your problem - the compiler can't resolve which amethod should be used for ambigious calls. If StringBuffer extended String or something else, there wouldn't be a problem - StringBuffer would be the more specific method - that's why it worked with String and Object - String extends Object so the String amethod is more specific.
Hope this helps,
Kathy
 
Rishi Yagnik
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank u
cathy i got u,it was nice explanation

Rishi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic