• 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

covariant returns

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what are covariant returns?
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Animal{}
class Dog extends Animal{}
public Animal doStuff()
{
return new Dog();
}

this is covariant return
 
Ranch Hand
Posts: 1880
Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot have two methods in the same class with signatures that only differ by return type. Until the J2SE 5.0 release, it was also true that a class could not override the return type of the methods it inherits from a superclass.
What this means is that a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass. This feature removes the need for excessive type checking and casting.

http://www.java-tips.org/java-se-tips/java.lang/covariant-return-types.html
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What this means is that a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.


So based on this quote this is a covariant return type.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Watch out! This is only true for reference types; not true for primitive types! Thus if a method returns an int, in superclass, and its overrided version, in the subclass, declares short to be the return-type, you will get an error. This is explained in the JLS as follows:-

Ch.9, Section 9.4.1
"If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (�8.4.6) for d2, or a compile-time error occurs."

Ch.8, Section 8.4.5
"A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2, if and only if the following conditions hold:-
  • If R1 is a primitive type, then R2 is identical to R1.
  • If R1 is a reference type then:

  • (i) R1 is either a subtype of R2 or R1 can be converted to a subtype of R2 by unchecked conversion (�5.1.9), or
    (ii) R1 = | R2 |.
  • If R1 is void then R2 is void.

  • Regards,
    Abdul Rehman.
    [ November 21, 2006: Message edited by: Abdul Rehman ]
     
    Cherry Singhal
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks a lott everybody!!
     
    reply
      Bookmark Topic Watch Topic
    • New Topic