permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused!!!!" Watch "confused!!!!" New topic
Author

confused!!!!

darshan karle
Greenhorn

Joined: May 09, 2008
Posts: 28
hi everybody,

I came across a program and it is compiling and running fine.
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}

public void method(String s)
{
System.out.println("String Version");
}

public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}

}
i dont know why it is printing String arg method statement not Object method Stmt.can anybody please explain

regards
darshan
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18760
    
  40

This is a common question that comes up very often here at the ranch. You can search for previous topics using "most specific method" or even via "AQuestion" (the name of the questions class).

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ben Zaidi
Ranch Hand

Joined: Apr 08, 2007
Posts: 151
Darshan,

When an object is instantiated, and when it invokes a method, it will
call the method having a more specific and nearer associated version.
Object class lies in the top hierarchy and is more Generic class, while
String version lies more close in hierarchy, so string version method
is called.

Hope it helps.

Ben


Ben,
--Lost in preparation of SCJP and SCWCD--
"Start writing a new chapter, for if you live by the book you'll never make history." (Ben Sobel)
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Hi Darshan

What Ben wrote is correct! You will understand a most important thing if you change your code as follows(which will ultimately does not compile because of the ambiguity):

public class AQuestion {
public void method(Integer o) {
System.out.println("Integer Verion");
}

public void method(String s) {
System.out.println("String Version");
}

public static void main(String args[]) {
AQuestion question = new AQuestion();
question.method(null);
}

}


The compile-time error is as follows:

The method method(Integer) is ambiguous for the type AQuestion

This makes you clear that the Integer and String has no relation at all! Both are from different hierarchies so for the compiler its not clear which method to invoke! But when you look at your example, it has two methods with the same name but different arguments method(Object o) and method(String s), for the compiler its clear that the String is a subclass of Object (as every class in Java is a subclass of Object) and for this reason the compiler invokes the more-specific version than the more generic version. I repeat, in the modified code above, the ambiguity is so clear and hence the compile-time error.

HTH.

Best regards
Kris
 
 
subject: confused!!!!