• 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

Overriding

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer is class B - return D

The explanation given in the mock answers is

Explanations : From J2SE 5.0 onwards. You are now allowed to override the return type of a method with a subtype of the original type.

My problem is that but class D does not come in the hierarchy so why do we get this answer?

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, till the JAVA SE5 the rule was: when you override a method, the overriding method has to return exactly the same type as the overridden one!
Beginning with JAVA SE5 the rule is: when you override a method, the overriding method has to return any type that is in relation IS-A with the type returned by the overridden method.

So, till JAVA SE5 i could not do this:


Beginning with JAVA SE5 this is legal, because Number and Integer are covariant return types.

So, in your code the class D extends the class C, so if the overridden method returns C, it's quite legal that the overriding method returns D.

Have a good day, Vadim.
 
Pinki Roy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vadim Vararu wrote:So, till the JAVA SE5 the rule was: when you override a method, the overriding method has to return exactly the same type as the overridden one!
Beginning with JAVA SE5 the rule is: when you override a method, the overriding method has to return any type that is in relation IS-A with the type returned by the overridden method.

So, till JAVA SE5 i could not do this:


Beginning with JAVA SE5 this is legal, because Number and Integer are covariant return types.

So, in your code the class D extends the class C, so if the overridden method returns C, it's quite legal that the overriding method returns D.

Have a good day, Vadim.




Thanks you so much Vadim, for clearing my concept.
 
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic