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 Doubt in overloaded method related question 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 "Doubt in overloaded method related question" Watch "Doubt in overloaded method related question" New topic
Author

Doubt in overloaded method related question

Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
Foe the following 2 code samples, i have confusion with code sample-2

Read the code below. Will be the result of attempting to compile and run the code below.

******************code sample-1************************
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.
Ans: C

*****************code sample-2 *************************

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);
}
}
Answers
1.The code does not compile.
2.The code compiles cleanly and shows "StringBuffer Version".
3.The code compiles cleanly and shows "String Version"
4.The code throws an Exception at Runtime.
Ans: A

Doubt: WHY the null is not accepted by String version of the overloaded method in code sample-2?

pls help
regards,
gitesh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Originally posted by Gitesh Ramchandani:
Doubt: WHY the null is not accepted by String version of the overloaded method in code sample-2?


When you compile a program, compilation errors are listed with error messages? What is the message that you got when you compiled it? And what about it do you not understand?

And .... Please Quote Your Sources.

Thanks,
Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abahmane mustafa
Greenhorn

Joined: Aug 22, 2007
Posts: 7
Originally posted by Henry Wong:


When you compile a program, compilation errors are listed with error messages? What is the message that you got when you compiled it? And what about it do you not understand?

And .... Please Quote Your Sources.

Thanks,
Henry



I'm also trying to understand ..wyh we get the compile time error :

C:\Program Files\Java\jdk1.5.0_11\bin>javac AQuestion.java
AQuestion.java:15: 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);
^
1 error


thanks by advance


Preparing for SCJP 5.0
Pradeep Meruva
Greenhorn

Joined: Aug 13, 2007
Posts: 7
It's been strange to me, why it is not calling String version of the method in the code sample -2


Thanks,
pradeep


Pradeep,<br />SCJP.
ranikkarasu mano
Greenhorn

Joined: Nov 07, 2005
Posts: 9
The first sample program object argument is overloaded by String object. So when you pass null as a parameter it calls String object method.

In sample2 It has Stringbuffer parameter and String object parameter. The null parameter has matched with two methods. So it confused. That's a reason it gives "method is ambiguous" error.
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
I did not understand....
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18824
    
  40

Originally posted by Alexsandra Carvalho:
I did not understand....


Basically, the compiler can't tell what type a null is. It can assigned to either a String or a StringBuffer reference. The specification states that when in doubt, the compiler will choose the most specific method. In this case, String and StringBuffer both inherits from object, so it can't choose.

Henry
Alexsandra Carvalho
Ranch Hand

Joined: Jul 13, 2007
Posts: 75
Hum..so good.
Thanks!
raghu nagabandi
Ranch Hand

Joined: Aug 14, 2007
Posts: 35
In first case there is relationship beaween Object and String class.
String is the subclass of Object.so compiler gives priority to String class.So first it will check whther the String accepts null or not. if it accepts no problem otherwise Object class handles this.

Where as in second case there is no relationship between String and StringBuffer. Both have same priority and both accepts null , so the compiler can not understand which one it has to call , so it raise an compile time error
Gitesh Ramchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 274
thanks for help.

Source:

Angelfire/Abhilash Quiz Q3&4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in overloaded method related question