“covariant return type” means ?

Hi friendzz,

We have return type in our methods, but what is covariant return type?
Let’s have an example.

Suppose that you have a class hierarchy in which Object is a superclass of java.lang.Number, which is in turn a superclass of ImaginaryNumber.

The class hierarchy for ImaginaryNumber

Object -> java.lang.Number -> ImaginaryNumber
Now suppose that you have a method declared to return a Number:

public Number returnANumber() {

}
The returnANumber method can return an ImaginaryNumber but not an Object. ImaginaryNumber is a Number because it’s a subclass of Number. However, an Object is not necessarily a Number — it could be a String or another type.

You can override a method and define it to return a subclass of the original method, like this:

public ImaginaryNumber returnANumber() {

}

Above means that we can return same or subclass type as the return type but not superclass type and this is called “covariant return type” .

following is the .java file of code with explanation..

import java.lang.Number;class ImaginaryNumber extends Number{

@Override
public int intValue() {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}

@Override
public long longValue() {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}

@Override
public float floatValue() {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}

@Override
public double doubleValue() {
throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.
}
}
class Myclass extends ImaginaryNumber{

// no error because we can return same as return type
public ImaginaryNumber returnANumber(){
ImaginaryNumber n1=null;
return n1;

}

// ERROR, we cannot return super class type as Number is the super class of ImaginaryNumber class
// public ImaginaryNumber returnANumber1(){
// Number n1= null;
// return n1;
//
//}
// ERROR, we cannot return super class type as Object is the super class of ImaginaryNumber class
// public ImaginaryNumber returnANumber1(){
// Object n1= null;
// return n1;
//
//}

// no error because we can return same as return type
public Number returnANumber2(){
Number n1=null;
return n1;

}

// no error because we can return subclass type as return type
public Number returnANumber3(){
ImaginaryNumber n1=null;
return n1;

}

// ERROR, we cannot return super class type as Object is the super class of Number class
// public Number returnANumber4(){
// Object n1=null;
// return n1;
//
//}

public static void main(String[] args) {

}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s