Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is Covariant return types

 
santhosh Gopalakrishna Bajja
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any one explain what is Covariant return types in simple terms...








santhosh
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try these tech tips from Sun...
  • Covariant Return Types
  • Covariant Parameter Types

  • [ April 21, 2006: Message edited by: marc weber ]
     
    Changchun Wang
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Covariant return types
    the overriding method's return type is subtype of the overriden method's return type
     
    santhosh Gopalakrishna Bajja
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 83
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    shashi nela
    Greenhorn
    Posts: 23
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 2023
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi, Shashi: your example is "cast" reference example.
    please check marc's links and Changchun's comments in this thread.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic