• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

mock test question about passing null as method parameter

 
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {

void someMethod ( Boolean obj ) {
System . out . println ( " Hello Boolean " ) ;
}

void someMethod ( Byte obj ) {
System . out . println ( " Hello Byte " ) ;
}

public static void main ( String args [ ] ) {
new Test ( ) . someMethod ( null ) ;
}
};



Options :

a . Compiles fine but throws an exception at runtime
b . Prints " Hello Boolean " when executed
c . Prints " Hello Byte " when executed
d . Compiler error - method call is ambiguous
e . Compiler error - no method matching someMethod ( null ) found


Output d . Compiler error - method call is ambiguous


Howcome we getting a compiler error when clearly Byte is more specific than Boolean.
Byte extends Number extends Object
Boolean extends Object

Please let me know how it works !!

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

Definitely the method call is ambiguous. null can be supplied to Byte
as well as Boolean. No method is more specific than the other to satisfy the
method call passing null as parameter.



sid sree
Howcome we getting a compiler error when clearly Byte is more specific than Boolean.
Byte extends Number extends Object
Boolean extends Object



It is not the way, you select more specific method.

Thanks,
 
Sudarshan Sreenivasan
Ranch Hand
Posts: 188
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

could you plese tell me what is technique used to find the more specific method because i had come acorss an example with a method(Obejct o) and method (String s)

Stating that method (String s) version is more specific because its lower in the object hierarchy !!

Please tell me where i am going wrong

Thanks
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See our SCJP FAQ: What is a most-specific method?
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sid,

have a look at the following discussion. The example is much more complicated, but it may be helpful.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic