Open In App

Covariant Return Types in Java

Last Updated : 03 Dec, 2021
Like Article

As the ear hit eardrums “overriding” we quickly get to know that it can be done either virtue of different datatypes or arguments passed to a function what a programmer learned initially while learning polymorphism in java.  Before JDK 5.0, it was not possible to override a method by changing the return type. When we override a parent class method, the name, argument types, and return type of the overriding method in child class has to be exactly the same as that of the parent class method. The overriding method was said to be invariant with respect to return type. 

Java version 5.0 onwards it is possible to have different return types for an overriding method in the child class, but the child’s return type should be a subtype of the parent’s return type. The overriding method becomes variant with respect to return type.

The co-variant return type is based on the Liskov substitution principle.

Now geeks you must be wondering about why to use for which we will be listing down the advantages as follows:

  • It helps to avoid confusing type casts present in the class hierarchy and thus making the code readable, usable and maintainable.
  • We get the liberty to have more specific return types when overriding methods.
  • Help in preventing run-time ClassCastExceptions on returns

Note: If we swap return types of Base and Derived, then above program would not work. Please see this program for example.

Example Two classes used for return types


// Java Program to Demonstrate Different Return Types
// if Return Type in Overridden method is Sub-type
// Class 1
class A {
// Class 2
class B extends A {
// Class 3
// Helper class (Base class)
class Base {
    // Method of this class of class1 return type
    A fun()
        // Display message only
        System.out.println("Base fun()");
        return new A();
// Class 4
// Helper class extending above class
class Derived extends Base {
    // Method of this class of class1 return type
    B fun()
        // Display message only
        System.out.println("Derived fun()");
        return new B();
// Class 5
// Main class
public class GFG {
    // Main driver method
    public static void main(String args[])
        // Creating object of class3 type
        Base base = new Base();
        // Calling method fun() over this object
        // inside main() method;
        // Creating object of class4 type
        Derived derived = new Derived();
        // Again calling method fun() over this object
        // inside main() method;


Base fun()
Derived fun()

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads