What is a covariant return type?

JavaOopCovariance

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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...

https://stackoverflow.com/questions/14694852/can-overridden-methods-differ-in-return-type

Solution 4 - Java

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();
  }

}

Solution 5 - Java

To add to the above answers, overriding is possible among co-variant return types, with the constraint that the return type of the overriding method (subclass method) should be a subclass of the return type of the overridden method (superclass method). This is valid from Java 5 onwards.

Solution 6 - Java

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.

Solution 7 - Java

  • 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

Solution 8 - Java

> - 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;
    }
}

Solution 9 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPopsView Question on Stackoverflow
Solution 1 - JavaAndrzej DoyleView Answer on Stackoverflow
Solution 2 - JavaMohamed ALOUANEView Answer on Stackoverflow
Solution 3 - JavaPresidentPratikView Answer on Stackoverflow
Solution 4 - JavaDhirajView Answer on Stackoverflow
Solution 5 - JavaLife Of PaiView Answer on Stackoverflow
Solution 6 - JavaKeshav GeraView Answer on Stackoverflow
Solution 7 - JavaArun RaajView Answer on Stackoverflow
Solution 8 - JavasnrView Answer on Stackoverflow
Solution 9 - Javauser11198642View Answer on Stackoverflow