Covariant return type

Covariant return type Means

Hi friend

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:

  1. public Number returnANumber()  
  2. {  
  3.     …  
  4. }  
The returnANumber method can return an ImaginaryNumber but not an Object. ImaginaryNumber is a Number because it’s 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
  1. public ImaginaryNumber returnANumber()  
  2. {  
  3.     …  
  4. }  
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..
  1. import java.lang.Number;  
  2. class ImaginaryNumber extends Number {@Override  
  3.     public int intValue()  
  4.     {  
  5.         throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.  
  6.     }  
  7.     @Override  
  8.     public long longValue()   
  9.     {  
  10.         throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.  
  11.     }  
  12.     @Override  
  13.     public float floatValue()   
  14.     {  
  15.         throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.  
  16.     }  
  17.     @Override  
  18.     public double doubleValue()   
  19.     {  
  20.         throw new UnsupportedOperationException(“Not supported yet.”); //To change body of generated methods, choose Tools | Templates.  
  21.     }  
  22. }  
  23. class Myclass extends ImaginaryNumber  
  24. {  
  25.     // no error because we can return same as return type  
  26.     public ImaginaryNumber returnANumber()   
  27.     {  
  28.         ImaginaryNumber n1 = null;  
  29.         return n1;  
  30.     }  
  31.     // ERROR, we cannot return super class type as Number is the super class of ImaginaryNumber class  
  32.     // public ImaginaryNumber returnANumber1(){  
  33.     // Number n1= null;  
  34.     // return n1;  
  35.     //  
  36.     //}  
  37.     // ERROR, we cannot return super class type as Object is the super class of ImaginaryNumber class  
  38.     // public ImaginaryNumber returnANumber1(){  
  39.     // Object n1= null;  
  40.     // return n1;  
  41.     //  
  42.     //}  
  43.     // no error because we can return same as return type  
  44.     public Number returnANumber2()  
  45.     {  
  46.         Number n1 = null;  
  47.         return n1;  
  48.     }  
  49.     // no error because we can return subclass type as return type  
  50.     public Number returnANumber3()   
  51.     {  
  52.         ImaginaryNumber n1 = null;  
  53.         return n1;  
  54.     }  
  55.     // ERROR, we cannot return super class type as Object is the super class of Number class  
  56.     // public Number returnANumber4(){  
  57.     // Object n1=null;  
  58.     // return n1;  
  59.     //  
  60.     //}  
  61.     public static void main(String[] args) {}  
  62. }  
Ebook Download
View all
Learn
View all