What is Covariant return type in java


In this core java tutorial we will learn what is Covariant return type in java.

Contents of page :
  • When was Covariant return type introduced in java?
  • What is Covariant return type in java?
  • Example of Covariant return type in java -
  • Full Program to show - Covariant return type in java -



Before learning about Covariant return type in java you must understand what is Method overriding in java.



When was Covariant return type introduced in java?
Covariant return type were introduced in java 5.

What is Covariant return type in java?
Java allow overriding by changing the return type, but only Covariant return type are allowed.

Example of Covariant return type in java -
return type of SuperClass’s myMethod is  SuperClass, and
Returns new SuperClass

return type of SubClass’s myMethod is  SuperClass, but
Returns new SubClass.


That means Covariant return type in java means that overriding method can return SubClass.

Example to show - Covariant return type in java -
class SuperClass{
   public SuperClass myMethod() {
          return new SuperClass();
   }
}
class SubClass extends SuperClass{
   @Override
   public SuperClass myMethod() {
          return new SubClass();
   }
}
This is absolutely legal method overriding in java.


Full Program to show - Covariant return type in java -
class SuperClass {
   public SuperClass myMethod() {
          System.out.println("In SuperClass myMethod()");
          return new SuperClass();
   }
}
class SubClass extends SuperClass {
   @Override
   public SuperClass myMethod() {        //Method overriding
          System.out.println("In SubClass myMethod()");
          return new SubClass();         //Covariant Return Type
   }
}
public class CovariantReturnTypesExampleInJava {
   public static void main(String[] args) {
          SuperClass obj = new SubClass(); //Create obj to access myMethod
          obj.myMethod(); //It will call myMethod, which will return Covariant Return Type
   }
}


When above program won’t be returning a Covariant Return Type -
class SuperClass {
   public SuperClass myMethod() {
          System.out.println("In SuperClass myMethod()");
          return new SuperClass();
   }
}
class SubClass extends SuperClass {
   @Override
   public SuperClass myMethod() {        //Method overriding
          System.out.println("In SubClass myMethod()");
          return new SubClass();         //Not a Covariant Return Type
   }
}




Summary >
In this core java tutorial we learned What is Covariant return type in java. Example of Covariant return type in java. Full Program to show - Covariant return type in java.



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>


Method overloading in java - in detail with programs,10 Features,need of method overloading, overloading main method, Diagram and tabular form of Implicit casting/promotion of primitive Data type



Method overriding in java - in detail with programs, 10 Features, need of method overriding, understanding @Override annotation, Covariant return, diagram to understand access modifiers


10 Difference between Method overloading and Method overriding in java - in detail with programs


1 comment:

  1. Before introducing covarient return type, how it was maintained ?
    Is there any other way to do same as this principle.?

    ReplyDelete