• 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

Return type problem

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal{}
class Gerbil extends Animal{}
class Vet{


Can somebody explain how check covariant return type as what i understand is
In line 7
Gerbil is the subclass so covariant return type is ok..but what to chek in the return type as return tyoe in line 7 is "return new Gerbil()" How does return type(return new Gerbil()) is related to "Gerbil go()"

as it says line 9 is wrong? I am a bit confused, so please give example to make me understand
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you take covariant return types out of your mind, will line 9 compile?? The answer is no as the method returns Gerbil so it can't return Animal class instance as Animal is super class of Gerbil. So for covariant return types, you just need to know that the return type of an overriding method can only be the save type as the overridden method or a sub-type (you know this). Other than that what you can return from the covariant method is as simple as a normal method...
 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit..I know it was simple but confusion lies everywhere..
Thks for clarification
 
this llama doesn't want your drama, he just wants this tiny ad for his mama
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic