What is covariant return type in Java?

Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. The covariant return type specifies that the return type may vary in the same direction as the subclass.

  1. class A{
  2. A get(){return this;}
  3. }
  4. class B1 extends A{
  5. B1 get(){return this;}
  6. void message(){System.out.println(“welcome to covariant return type”);}
  7. public static void main(String args[]){
  8. new B1().get().message();
  9. }
  10. }
Output: welcome to covariant return type