HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
1. Feel Free to ask and submit anything on and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.

corejava-basics: What is covariant return type?

Hello guys i am little bit confused about covariant return type. Please explain it with suitable example.

corejava x 353
basics x 171
Posted On : 2013-11-23 22:05:27.0
profile Rishi Kumar - Rishi Kumar


The covariant return type specifies that the return type may vary in the same direction as the subclass. Before Java5, it was not possible to override any method by changing the return type. But since Java5, it is possible to override method by changing it´s return type. if subclass overrides any method whose return type is Non-Primitive but it changes its return type to subclass type. Let´s take a simple example:
Note: If you are beginner to java, skip this topic and return to it after OOPs concepts.
****************** Simple example of Covariant Return Type ********************

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

class B extends A{
B get(){return this;}
void message(){System.out.println("welcome to covariant return type");}

public static void main(String args[]){
new B().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.

Posted On : 2013-11-23 22:13:25
Satisfied : 1 Yes  0 No
profile Garima Gupta - Garima Gupta
Reply This Thread

Post Answer
Please Login First to Post Answer: Login login with facebook -