• 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

Question on OverLoading

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Can any one explain for the following odd behaviuor?
a) When func method is overloaded for Object and String and called sending a null the String version is executed.
b)When the same func method is overlaoded for String and StringBuffer and null called using null, compiler complains about ambiguity.
Here is the code
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "most specific" override will be called. When you have Object and String, the one that takes a String will be called because a String is an Object. String is the "most specific".
When you add StringBuffer there are now two methods to choose from. String and StringBuffer are both distinct Objects. The compiler cannot choose between the method that takes a String and the method that takes a StringBuffer. So you get the ambiguity.
Sort of...
-Barry
[ January 28, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another way to put it is:
There will be no ambiguity if the arguments are in the same hierarchical path.
In the following code only m(CE c) creates ambiguity. The arguments of the rest are on the same hierarchical path and they don't. You'll have to take out either lines 1,2,3,4 or 5 only for the code to compile.
Like Barry said the most specific "overload" will be called.
By the way Barry I believe this is an "overload" rather than an "override".
 
I have always wanted to have a neighbor just like you - Fred Rogers. 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