• 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

Call to overloaded method with null reference

 
Ranch Hand
Posts: 105
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I came across this piece of code on one of the mock exams.

The output is "My String". The explanation was "when null is passed as an argument to the method myMethod(String s) will be called. Had myMethod(String s) not been present, myMethod(Object o) would be called". But that does not explain why the the overloaded method with the String argument is chosen, instead of the one with the Object argument... Do you know?

Thanks in advance,

Johan Pelgrim
The Netherlands
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Johan

Most Specific method will be callen rather than General method

public void myMethod(char c[])
{
System.out.println("My String");
}

-> Add this code in ur program and enjoy the output
 
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose in case of char[] array, which is an object, the new method will be called, is that right?
 
Johan Pelgrim
Ranch Hand
Posts: 105
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aha!

I got a compile error saying both the method with the String s argument and the char[] c argument match. I.e. the compiler cannot determine which method it should run with the null-reference. They're both on the same level.

So, given


This code will fail


But this will compile and will run, as you say, the most specified method!



And will give output "My ObjectSubClass1SubClass"!

Cool! Thanks much Vidya!

[ May 26, 2005: Message edited by: Johan Pelgrim ]
[ May 26, 2005: Message edited by: Johan Pelgrim ]
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, null can be a referrence for any object, here it Object or String. Since java.lang.String extends java.lang.Object, the compiler assumes the null is used for String object.
 
Where does a nanny get ground to air missles? Protect 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