Covariant Return Type

    Covariant Return Type

    This return type specifies that the return type of the method may vary in the child class. Earlier, it was not possible that you can change the return type of the method while implementing method overriding. But now it is possible to override a method with its return type changed. It is possible only if the return type of the method in the parent class is non-primitive then the return type will get changed to the child class return type.


    class Demo{  
        Demo get(){return this;}  
    class Sub extends Demo{  
        Sub get(){return this;}  
        void message(){System.out.println("Welcome");}  
        public static void main(String args[]){  
            new Sub().get().message();  



    In the above example, both the get() methods have different return types but implementing method overriding, this is known as the covariant return type.

    How to implement Covariant return type

    Instead of Java, JVM supports the return type based method overriding. JVM checks for the complete signature including the return type of the method which means you can declare two or more methods differentiated on the bases on the return type. This is how java implements covariant return types.