A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Overloading query
Abhinaba Basu
Greenhorn
Joined: Jun 06, 2006
Posts: 15
posted
Jul 21, 2006 22:36:00
0
My code is given below:
public class Main{ public static void main(String[] args) { new Main().A(null); } public void A(String str){ System.out.println("String"); } public void A(Object obj){ System.out.println("Object"); } }
And it is printnig "String"
And my next code is give below:
public class Main{ public static void main(String[] args) { new Main().A(null); } public void A(String str){ System.out.println("String"); } public void A(StringBuffer obj){ System.out.println("StringBuffer"); } }
Why it is doing like this? Can anybody explain me?
<b>Abhinaba basu</b><br />Software Engineer,<br /><a href="http://"http://www.kaizen-it.com"" target="_blank" rel="nofollow">Kaizen Informatics Pvt Ltd</a><br /><a href="http://"http://engineers.bizhat.com"" target="_blank" rel="nofollow">My site</a> | <a href="http://"http://theabhinaba.bizhat.com"" target="_blank" rel="nofollow">My Blog</a> | <a href="http://"http://www.kaizen-it.com"" target="_blank" rel="nofollow">My office</a>
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
I like...
posted
Jul 21, 2006 23:01:00
0
See if this helps:
Most specific method
.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award."
~Joe Strummer
sscce.org
vidya sagar
Ranch Hand
Joined: Mar 02, 2005
Posts: 580
posted
Jul 21, 2006 23:02:00
0
Normally in this scenario,More appropriate matching method will be called.
In the First Case,
String
is more appropriate. so the method with string argument is called.
But in the Second Case,String and
StringBuffer
is in same level so the compiler cannot know which one to choose.So it throws ambiguous error.
I agree. Here's the link:
http://aspose.com/file-tools
subject: Overloading query
Similar Threads
calling constructor with null ?
null as a parameter to method
Can we create Object class?
Method with null as arg
How to determine which constructor is called?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter