This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which method to use?

 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from sanjayahuja.tripod.com (with modifications)

I thought this would be a compile error, 'coz method(null) would be ambiguous (it wouldn't know which method() will be invoked.) But it compiles and prints out "Integer Version" when run! Also, when method(String) is uncommented out, then the compiler complains about ambiguous methods.
 
Tybon Wu
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the inheritance tree:
Object
|
+-Number
++|
+++-Integer
Since null is compatible to any object, the one furthest down the compatible path will be chosen, in this case, Integer.
If you also have the overloaded method with the String parameter, then that will create two compatible paths for null, which causes the ambiguous methods error.
[ June 19, 2002: Message edited by: Tybon Wu ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic