File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes why this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "why this?" Watch "why this?" New topic

why this?

Rishi Yagnik
Ranch Hand

Joined: Jan 04, 2001
Posts: 84
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();
after compiling i am getting ans as String version can some one explain me why?
Rob Acraman
Ranch Hand

Joined: Dec 03, 2000
Posts: 89
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

Joined: Jan 04, 2001
Posts: 84
Thanks Rob
i got u infact i have got the same concept in my mind but thanks for Explanation.
I agree. Here's the link:
subject: why this?
It's not a secret anymore!