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

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.
 
Stop it! You're embarassing me! And you are embarrassing this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic