Question

What is a covariant return type in Java? In object-oriented programming in general?

Was it helpful?

Solution

Covariant return, means that when one overrides a method, the return type of the overriding method is allowed to be a subtype of the overridden method's return type.

To clarify this with an example, a common case is Object.clone() - which is declared to return a type of Object. You could override this in your own class as follows:

public class MyFoo
{

   ...

   // Note covariant return here, method does not just return Object
   public MyFoo clone()
   {
       // Implementation
   }
}

The benefit here is that any method which holds an explicit reference to a MyFoo object will be able to invoke clone() and know (without casting) that the return value is an instance of MyFoo. Without covariant return types, the overridden method in MyFoo would have to be declared to return Object - and so calling code would have to explicitly downcast the result of the method call (even thought both sides "know" it can only ever be an instance of MyFoo).

Note that there's nothing special about clone() and that any overridden method can have a covariant return - I used it as an example here as it's a standard method where this is often useful.

OTHER TIPS

Here is another simple example :

Animal class

public class Animal {

    protected Food seekFood() {

        return new Food();
    }
}

Dog class

public class Dog extends Animal {

    @Override
    protected Food seekFood() {

        return new DogFood();
    }
}

It’s possible to modify the return type of the Dog’s seekFood() method to DogFood - a subclass of Food, as shown below:

@Override
protected DogFood seekFood() {

    return new DogFood();
}

That’s perfectly a legal overriding, and the return type of Dog’s seekFood() method is known as covariant return type.

From the release of JDK 1.5, covariant types were introduced in Java. and I'll explain it to you with a simple case, : When we override a function the function is allowed to make changes to it's behaviour that's what you get to read in most of the books, but what they { authors } miss out on is that we can change the return type too. check below link for clarification we can change the return type as long as it can be assigned to return type of Base version of the method.

So this feature of returning derived types is called COVARIANT...

Can overridden methods differ in return type?

covariant Return types simply means returning own Class reference or its child class reference.

class Parent {
 //it contain data member and data method
}

class Child extends Parent { 
//it contain data member and data method
 //covariant return
  public Parent methodName() {
     return new Parent();
          or 
     return Child();
  }

}

Covariant return type specifies that the return type may vary in the same direction as the subclass

class One{  
    One get(){return this;}  
}  

class Two extends One{  
  Two get(){return this;}  

void message(){
  System.out.println("After Java5 welcome to covariant return type");
}  

public static void main(String args[]){  
    new Two().get().message();  
}  
}

Before Java 5, it was not possible override any method by changing the return type. But now, since Java5,

it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type.

  • It helps to avoid confusing type casts present in the class hierarchy and thus making the code readable, usable and maintainable.
  • We get a liberty to have more specific return types when overriding
    methods.

  • Help in preventing run-time ClassCastExceptions on returns

reference: www.geeksforgeeks.org

  • The covariant return type in java, allows narrowing down return type of the overridden method.
  • This feature will help to avoid down casting on the client side. It allows programmer to program without the need of type checking and down casting.
  • The covariant return type always works only for non-primitive return types.
interface Interviewer {
    default Object submitInterviewStatus() {
        System.out.println("Interviewer:Accept");
        return "Interviewer:Accept";
    }
}
class Manager implements Interviewer {
    @Override
    public String submitInterviewStatus() {
        System.out.println("Manager:Accept");
        return "Manager:Accept";
    }
}
class Project {
    public static void main(String args[]) {
        Interviewer interviewer = new Manager();
        interviewer.submitInterviewStatus();
        Manager mgr = new Manager();
        mgr.submitInterviewStatus();
    }
}

Other example is from Java,

UnaryOperator.java

@FunctionalInterface
public interface UnaryOperator<T> extends Function<T, T> {

    /**
     * Returns a unary operator that always returns its input argument.
     *
     * @param <T> the type of the input and output of the operator
     * @return a unary operator that always returns its input argument
     */
    static <T> UnaryOperator<T> identity() {
        return t -> t;
    }
}

Function.java

@FunctionalInterface
public interface Function<T, R> {

    ........
    ........
    ........
    ........

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

Before Java5, it was not possible to override any method by changing the return type. But now, since Java5, it is possible to override method by changing the return type if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top