• 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 exam inheritance/varargs question

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am stumped by a question in Osman Mrzljak's SCJP Java 5 Mock Exam (http://home.wanadoo.nl/mrzljak/en/biz/exam.html). It is question 8:



a: Compile and output CiaoHallo
b: Compile and output CiaoOla
c: Compile and output HalloHallo
d: Compile and output HalloOla
e: This will not compile, becouse of ambiguous method reference to doSomething...

I expected the answer to be b, but the correct answer is d. I've compiled and run the code to verify, but I don't understand. Why doesn't the doSomething method in Raptor override the varargs doSomething method in Bird?

Thanks for any insight!
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We'll thats because these are over loaded methods and not over-ridden methods, in an overloaded method the object reference type is considered, here its Bird, the object for the purpose of over loading does'nt even know that the Raptor dosomething() method exists. Hope this helps.

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

a nice question, that is.

for option b) the code has to be like this...

 
chris java
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that helps!

The part that stumps me is thinking about how the compiler determines when it's appropriate to consider a method overridden in a subclass. Based on your responses, it looks like the compiler can only consider a method overridden if the arguments are exactly the same. So even though a varargs method argument can accept 0+ arguments, (int i, int ...args) is still distinct from (int i). Is that correct?
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chris,

So even though a varargs method argument can accept 0+ arguments, (int i, int ...args) is still distinct from (int i). Is that correct?



Yes They signature should be exactly the same, and here these two signatures are distinct.



(...but what in case of int... and int[]? I just wanted to check out this, if int... can be overridden by int[] or vice versa. Guess what, the compiler warns that overriding won't happen but results suggest that overriding does happen!!?? Wats happening!!)

Here's the code:

[ January 16, 2008: Message edited by: Vishwanath Murthi ]
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am sorry to say that your program shoots a runtime exception of "No such method found error and it gives a warning as you have mentioned

sorry if i am wrong .please do compile and see

----------------------------
SCJP 5.0 preparing
--------------------------
 
Vishwanath Krishnamurthi
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

possibility that you executed it as, java Over instead of java Bover??


[I did like this the first time ]
 
Akshay Dashrath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chris Java:
Thanks, that helps!

The part that stumps me is thinking about how the compiler determines when it's appropriate to consider a method overridden in a subclass. Based on your responses, it looks like the compiler can only consider a method overridden if the arguments are exactly the same. So even though a varargs method argument can accept 0+ arguments, (int i, int ...args) is still distinct from (int i). Is that correct?



Yes thats correct, if youre're reffering to the Kathy Sierra and Bert Bates book refer to page 154, the differences between Overloading and Overriding are outlined very clearly
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic