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 null arg in overloaded method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "null arg in overloaded method" Watch "null arg in overloaded method" New topic
Author

null arg in overloaded method

Nidhi Juneja
Greenhorn

Joined: Oct 07, 2002
Posts: 27
Hi everyone here is another code whose reult is unclear
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);
}
}
O/P String Version
While this code:
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);
}
}
O/P-reference to method is ambiguous, both method method(java.lang.StringBuffer) in AQuestion and method method(java.lang.String) in AQuestion match
question.method(null);

How r these 2 situations different? I am missing some point?
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
In the 1st case, the String method is called because it is more specific than the Object method.
In the original code, neither the String method nor the StringBuffer method was more specific, so the compiler complained.


Sunita<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Refer to the thread
http://www.coderanch.com/t/239328/java-programmer-SCJP/certification/null-argument-method-being-passed
http://www.coderanch.com/t/239338/java-programmer-SCJP/certification/Why-null-not-common-object
Nidhi Juneja
Greenhorn

Joined: Oct 07, 2002
Posts: 27
Thanks Sunita...now i have understood the concept...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null arg in overloaded method