Win a copy of Functional Reactive Programming this week in the Other Languages forum!

what is Covariant return types

santhosh Gopalakrishna Bajja
Ranch Hand
Posts: 34
Can any one explain what is Covariant return types in simple terms...

santhosh

marc weber
Sheriff
Posts: 11343
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
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
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
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
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
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
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
hi, Shashi: your example is "cast" reference example.