Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Doubt in overloaded method related question

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's been strange to me, why it is not calling String version of the method in the code sample -2


Thanks,
pradeep
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not understand....
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hum..so good.
Thanks!
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for help.

Source:

Angelfire/Abhilash Quiz Q3&4
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic