Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

here is a small doubt

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody,

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);
}
}

This is the program.It is giving output as String version. I thought it will give compiletime error,bcoz of ambiguity.

Can anyone explain the reason why it is giving string version.? why isnt calling the method with Object as argument?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler makes the most specific mapping it can if there are multiple methods that can be called. In this case, the method whose parameter list is String is more specific that the one whose parameter list is Object.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way i think it is, if there was only one method which took a parameter String or Object, then it would compile fine. It will work fine if you had both signatures
1>> method(Integer o)
and method(Object o)
it wont compile for
2>> method(Integer o)
and method(String o)

It wont compile if you have only one method with the following signature
method(Integer o)

In the first case though, the method it will select at runtime is the one that corresponds to Integer,

because every object reference can be null, so JVM will choose the most specific method.

Now if you try it with three methods,
method(Integer o)
method(Object o)
method(String o)
then it will however give compile time error.
 
aditya makam
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thank you very much for your reply.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Aditya,
The reason y there is no compiler error because String is a subclass of Object hence String is more specific than Object. Now null can be passed as argument for both Object and String argument methods, but the compiler selects the most specific one which is String method hence it is executed. But if the compiler cannot find out which method is more specific then it gives a compiler error.
for example method(Object o);
method(String o);
method(Integer o);
Here both String and Integer are subclass of Object hence they are more specific than Object so one of these 2 methods has to be called. But between of String and Integer there is no inheritence relationship hence it is impossible to tell which among these 2 methods is more specific by the compiler. Hece compiler error occurs stating ambiguous call.
Please let me know if further clarifications are needed.
Yours Sreeram
 
reply
    Bookmark Topic Watch Topic
  • New Topic