---
title: "What is covariant return type in java?"  
description: "What is covariant return type in java?"  
author: "Royce Roy"  
published: 2015-03-31  
updated: 2020-09-20  
canonical: https://www.mindstick.com/interview/2382/what-is-covariant-return-type-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# What is covariant return type in java?

The covariant return type specifies that the return type may vary in the same direction as the subclassBefore 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\

```
class A{  A get(){return this;}  }    class B1 extends A{  B1 get(){return this;}  void message(){System.out.println("welcome to covariant return type");}    public static void main(String args[]){  new B1().get().message();  }  }
```

\
**Output:**welcome to covariant return type\
\
As you can see in the above example, the return type of the get() method of A class is A but the return type of the get() method of B class is B. Both methods have different return type but it is method overriding. This is known as covariant return type\
\

## Answers

### Answer by Anonymous User

The covariant return type specifies that the return type may vary in the same direction as the subclassBefore 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\

```
class A{  A get(){return this;}  }    class B1 extends A{  B1 get(){return this;}  void message(){System.out.println("welcome to covariant return type");}    public static void main(String args[]){  new B1().get().message();  }  }
```

\
**Output:**welcome to covariant return type\
\
As you can see in the above example, the return type of the get() method of A class is A but the return type of the get() method of B class is B. Both methods have different return type but it is method overriding. This is known as covariant return type\
\


---

Original Source: https://www.mindstick.com/interview/2382/what-is-covariant-return-type-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
