• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overloading

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
it is printing string version. why the compiler does not say ambiguous method invocation? why string version is called?
Thanks
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use the UBB-code-tag in the future; it makes the code much easier to be read.
Now to your question: The JVM is looking for the most specialized method, which will "fit". Here it is easy: null is allowed for String as for Object, but Object is the father of String, so take the string-method.
If you add

you will get a compile time error, because null is allowed for Object, String AND Vector, but the compiler cannot decide if the String-method or the Vector-method is right. Both, String AND Vector, are derived from Object. None of them is "more special".
Hope it helps
Detlev
[This message has been edited by Detlev Beutner (edited July 16, 2001).]
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler won't complain because the signatures of the two methods are different.
method(String) is called first since the String class is a descendant of the Object class.
 
Detlev Beutner
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dan: No, that's definitely /not/ the reason. Look at my example, also with the third method, you will have three different signatures. But the compiler checks the call of method, which has an constant as parameter, and sees: Oh, I don't know which method to call, which is meant?
Hope it clarifies
Detlev
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offence Dan but try the code . This helps me understand things better . Anyway here's the error generated by Det's block of code --
---------- javac ----------
AQuestion.java:19: Reference to method is ambiguous. It is defined in void method(java.lang.String) and void method(java.util.Vector).
question.method(null);
^
1 error
Normal Termination
Output completed (3 sec consumed).
Peace !
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think ashit is right, u should always try out codes b4 u jump the gun.
Bhaskar
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
Well i fail to understand the discussion going excepth the original question by Priya Rajan .
What i have understand by the Priya's question is as follows.
Object class is a superclass of every class and a/c to this regard all the subclass of Object are specialized version of Object.String is also a subclass & hence it is specialized version of Object.When nullis passes as an argument to method, compiler checks for the mostsuitable version of method in which argument is fitted.
In this case compiler finds String to be the most suitable candidate for accepting null.This is what i conclude answer while reading the code first time.
Regards,
Hassan.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic