• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

AQuestion

 
Ranch Hand
Posts: 34
  • 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);
}
}
Output of the above code is..
String Varsion
im bit confused pls help
regards
Jaya Murugan
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
java.lang.Object is at the root of the hierarchy and since all classes extend java.lang.Object, at runtime the argument "null" is passed to the most specific method that has subclasses of java.lang.Object as a parameter.
In the code sample, if the method that has String as a parameter is commented out then "Object Version" gets printed.
You may try the code mentioned below.
...
public void method(String s)
{
System.out.println("String Version");
}
public void method(String [] s1)
{
System.out.println("String Array Verion");
}
...

Here I 've included a method that has a String [] as a parameter.
This may result in Compile time error with the meassge
"reference to method is ambiguous".
Since both the methods have "SAME SIGNATURE" i.e,with same method name and same parameter type, the compiler will not know which method to invoke and hence throws a compile time error stating that reference to the method is ambiguous.
- Suresh Selvaraj
 
Jaya Murugan
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI SURESH..
GOT IT
THNAKS
JAYA MURUGAN
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic