my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boxing method call doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "boxing method call doubt" Watch "boxing method call doubt" New topic
Author

boxing method call doubt

dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383


What will be the output for the above program?
1)Object called
2)Number called-answer
3)Compiler Error
4)Runtim Exception


Why the answer is Number? I thought it is ambiguous between Object & Number. So the answer is compiler error.


SCJP-1.5<br />SCWCD-1.4
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, it's not ambiguous.

The call matches both methods, but the one with parameter Number is more specific than the one with parameter Object. The most specific method that matches will be chosen in this case.
dolly shah
Ranch Hand

Joined: Jun 18, 2007
Posts: 383
Thanks. I just forgot.
Yogvinder Singh Ghotra
Ranch Hand

Joined: Sep 10, 2007
Posts: 38
Can someone explain this to me in detail??
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
Here the int literal 10 is first boxed to Integer which is then widened to Number.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

When two methods match the call, Java chooses the most specific method. See section 15.12.2.5 of the Java Language Specification. In this case, the method that takes a Number is more specific than the method that takes an Object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
jQuery in Action, 2nd edition
 
subject: boxing method call doubt
 
Similar Threads
Question Regarding Boxing
Boxing/conversion question. Thanks.
Boxing null literal
Boxing Doubt
Method with null as arg