File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Method Overloading 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 "Method Overloading" Watch "Method Overloading" New topic
Author

Method Overloading

Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Hi guys.....
I found this question on Abhilash's mock exam:
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);
}
}

Answers
1. The code does not compile.
2. The code compiles cleanly and shows "Object Version".
3. The code compiles cleanly and shows "String Version"
4. The code throws an Exception at Runtime.
I compiled the code and excuted it and it gives an output of "String Version".
What I did not understand is how??? I know that a null can be assigned to any reference type but how does the JVm know which method to invoke
More particularly, in the above case why did the method which takes a String argument get invoked and why not the method which takes an object as argument.???
This one shook me up!!!
Thanks....
ricky gonzalez
Ranch Hand

Joined: Jun 30, 2000
Posts: 167
I think when you run it, JVM actually tried to decide whether null is a String value or an Object value. And for some reason it was decided that null is a String in this case. Personally, I think JVM chooses the broader type as default. I am just guessing.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I executed this code it is giving "String version" for me also.
I was expecting that it should give NullPointerException, shouldn't it.....
Can anybody explain it more clearly , as it is very confusing for me.
thanks in advance,
DS
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think what is happening here is that the most derived version is getting called. I experimented with adding the following:
public void method(AQuestion a)
{
System.out.println("AQuestion Version");
}
and I got a compilier error (ambiguous call...). Since both String and AQuestion derive from Object--but from different paths--the compilier can't decide.
Then I added a subclass AQuestionSub, added an overloaded method() for that class, and removed the call to the String version of method()...
public void method(AQuestion a)
{
System.out.println("AQuestion Version");
}
public void method(AQuestionSub a)
{
System.out.println("AQuestionSub Version");
}
and the compiler chose the AQuestionSub version as I hoped.
I can't authoritatively (sp?) explain this behavoir, but I think it makes some logical sense that, in the original program, the String version is called instead of the Object. That is, the null can be assigned to either Object or to String, so the compiler picks the most derived version. Someone please correct me if you have an official explanation.
Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
Still confuesd with this one:
The following piece of code is similar to the above one.... and it fails to compile....
public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer 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 compiler gives an error saying:
C:\java\bin>javac AQuestion.java
AQuestion.java:23: Reference to method is ambiguous. It is defined in void method(java.lang.String) and void method(java.lang.StringBuffer). question.method(null);
^
1 error
Can someone explain this please???
Thanks.....
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
From an intuitive standpoint, I think the error makes sense. If you were the compiler, which version of the overloaded method would you decide to use? Or is it perhaps ambiguous?? Remember, both String and StringBuffer can be assigned a null, so either one will do.
Does that not make any sense to you?
Shafeeq Sheikh
Ranch Hand

Joined: May 12, 2000
Posts: 68
DoB... In that case, if we refer to my first piece of code, one can also argue that 'null' can be assigned to String as well as to Object.
Then how come the compiler does not complain???
completely :confused
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Yeah, I certainly agree that it's confusing. What I think is happening in the original code is the compiler matched the call to the most derived type. Since String is a subclass of Object, it is clearly more derived than Object. True that Object and String can both be assigned null, but String is chosen based on the above reasoning. And, since there's no other, competing version of the method (taking a clss derived from Object but not related to String), we don't have ambiguity.
I know that this is really just conjecture on my part, but the above represents the behavoir. It would be nice to find some documentation on the definitive rules the compiler uses to resolve function calls (all types: overloaded, overriden...)
ppathak
Greenhorn

Joined: Jul 06, 2000
Posts: 19
well in first case bcos Object is parent class of String , & when u call with null as parameter , as String is subclass of Object, the method with String as parameter is called.
When u write 2 methods one with String & other with StringBuffer
as parameter, none of each is parent of other & also each of them can take null , so u get compile error saying ambiguous reference.
hope this helps u
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Overloading
 
Similar Threads
about null
Mock test Q
about overloading
question in javaquiz
Constructor Overloading