• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 9697
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
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic