This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes covariant returns Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "covariant returns" Watch "covariant returns" New topic
Author

covariant returns

Cherry Singhal
Greenhorn

Joined: Sep 05, 2006
Posts: 6
what are covariant returns?
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
class Animal{}
class Dog extends Animal{}
public Animal doStuff()
{
return new Dog();
}

this is covariant return


SCJP 5.0 77%
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

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


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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.


~Sanjeev Singh<br />SCJP 1.5
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
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 ]

    SCJP 5.0 (100%)
    Cherry Singhal
    Greenhorn

    Joined: Sep 05, 2006
    Posts: 6
    Thanks a lott everybody!!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: covariant returns
     
    Similar Threads
    Covariant Returns
    Overriding Vs Redefinition
    query related overriding.
    including covariant return ?
    Need help for method selection