In this tutorial, we'll discuss covariant return type in Java which was added in Java 5. Method overriding in Java is said to be covariant with respect to return type which means that the return type is allowed to vary in the same direction as the subclass. What is covariant return type in Java First let’s try to understand what exactly does this covariant return type mean? Before Java 5 it was not possible to change the return type of the overridden method in the sub-class meaning the method in the parent class and the overridden method in the child class should have the same name, same number and type of arguments and same return type. Java 5 onward, because of this covariant return type feature, it is possible for the overridden method to have different return type from the method in the parent class. There is one restriction though, the return type of the sub-class method must be a subtype of the return type of the parent class method . For example - In parent class the
Java, Spring, Web development tutorials with examples