This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Mock Exam Errata and the fly likes why complie time error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "why complie time error?" Watch "why complie time error?" New topic
Author

why complie time error?

Rishi Yagnik
Ranch Hand

Joined: Jan 04, 2001
Posts: 84
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

Joined: Jan 04, 2001
Posts: 84
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

Joined: Aug 04, 2000
Posts: 103
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

Joined: Jan 04, 2001
Posts: 84
Thank u
cathy i got u,it was nice explanation

Rishi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why complie time error?
 
Similar Threads
from mock exam qs..Jquest
why this?
While
Why Error Here?
why the overriding not working???