This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is Covariant return types Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is Covariant return types" Watch "what is Covariant return types" New topic
Author

what is Covariant return types

santhosh Gopalakrishna Bajja
Ranch Hand

Joined: Apr 01, 2006
Posts: 34
Can any one explain what is Covariant return types in simple terms...








santhosh


"Nothing is impossible even impossible has I 'M POSSIBLE"
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Try these tech tips from Sun...
  • Covariant Return Types
  • Covariant Parameter Types

  • [ April 21, 2006: Message edited by: marc weber ]

    "We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
    sscce.org
    Changchun Wang
    Ranch Hand

    Joined: Feb 15, 2006
    Posts: 83
    Covariant return types
    the overriding method's return type is subtype of the overriden method's return type
    santhosh Gopalakrishna Bajja
    Ranch Hand

    Joined: Apr 01, 2006
    Posts: 34
    class A{
    int x=5;
    }

    class B extends A {
    int x=6;
    }


    public class CovariantTest {
    public A getObject() {
    return new A();
    }

    public static void main(String[] args) {
    CovariantTest c1 = new SubCovariantTest();
    System.out.println(c1.getObject().x);
    }
    }


    class SubCovariantTest extends CovariantTest {
    public B getObject() {
    return new B();
    }
    }



    It returns 5 why not 6.

    Does this mean Covarient return types are similar to static methods.

    Two methods with different return types one in superclass and one in subclass. method return type should be subtype of superclass method return type is what i understood.

    Confussing Boss Please Explain me what is happening.

    correct me if i am wrong.......

    santhosh
    [ April 21, 2006: Message edited by: santhosh Gopalakrishna Bajja ]
    santhosh Gopalakrishna Bajja
    Ranch Hand

    Joined: Apr 01, 2006
    Posts: 34
    Compare this example with the previous example.

    interface Inter{}

    class First implements Inter {
    public Inter Method1(){
    System.out.println("Super");
    return new First();
    }
    }


    class Second extends First {
    public First Method1(){
    System.out.println("Sub");
    return new Second();
    }
    }


    public class CovariantTest3 {
    public Static void main(String[] args) {
    First first = new Second();
    Inter First1 = first.method();
    }
    }


    It prints Sub.

    Why is it printing Sub why not Super.

    Please compare the last post and this post and help me to solve what exactlly is covarient return types...



    santhosh
    Changchun Wang
    Ranch Hand

    Joined: Feb 15, 2006
    Posts: 83
    you can compare the code

    notes the problem is no relation to conviant return types
    and found the trouble on field and method
    Mohammed Yousuff
    Ranch Hand

    Joined: Apr 18, 2006
    Posts: 45
    Covarient Return Type:

    the Covarient return type will allow to override the Super class method with the derived class method,where the Return type must be sub class of the Super class method Return Type.


    Regards<br /> <br />Mohammed Yousuff M N <br /> <br />Try NOT to Become a man of SUCCESS, BUT Try to Become a man of VALUE..
    shashi nela
    Greenhorn

    Joined: May 24, 2006
    Posts: 23
    Hi,

    this is my first reply , so if i make any mistakes please correct me.

    ex:

    public class Super
    {

    }

    public class Sub extends Super
    {

    }

    public class TestCovariant
    {
    Sub s = Sub();
    public Super checkReturnType()
    {

    return s;
    }
    }

    The key is that the method checkReturnType() is of type Super. But Sub reference s is returned, Since Sub is a sub class of Super we can return the Sub object. This is true as os jdk 1.5 but when we use as jdk 1.4 then it gives an error.

    Hope this is correct and useful!!!

    regards,
    shashi
    wise owen
    Ranch Hand

    Joined: Feb 02, 2006
    Posts: 2023
    hi, Shashi: your example is "cast" reference example.
    please check marc's links and Changchun's comments in this thread.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: what is Covariant return types
     
    Similar Threads
    Benefits of covariant return types?
    Covariant return Types
    Covariant
    Doubt-Generics and Collections-Kathy Sierrra- SCJP6-Question8
    Covariant return types