What is covariant return type in Java?

Java Programming Java8Object Oriented Programming

This article will help you understand what a covariant return type in Java Programming language is.

Covariant Return Type

The Covariant return type is one type of method overriding where the return type of a method may vary in the same direction as that of the subclass. Since Java 5, it is possible to implement method overriding using the covariant return type. Let’s see an example of Covariant Return Type.

Example

class Trial { // child class declaration Trial get() { // child class method return this; } } public class CovariantExample extends Trial { // main class inheriting child class @Override CovariantExample get() { // parent class method return this; } void message() { // method to display System.out.println("Welcome to TutorialsPoint"); } public static void main(String args[]){ // main function declaration new CovariantExample().get().message(); // calling parent class member function to display } }

Output

Welcome to TutorialsPoint

Here, we see that return types of class Trial and CovariantExample are different but at the end we override the method get (). This is covariant return type.

Advantages of Covariant Return Type

Covariant return type makes the code more user-friendly and maintainable by removing all confusing type casting.

  • It provides the liberty to have more to the point return types.

  • It helps in preventing runtime ClassCastExceptions from happening while return is used.

Example

Let us understand Covariant Return Type using an example.

class B1 { // parent class declaration B1 trial(){ // function with similar return type to that of the other child class trial() functions return this; } void print() { // display function System.out.println("Inside the class B1"); } } class B2 extends B1 { // child class B2 inherits parent class B1 @Override B2 trial() { // function with similar return type to that of the other child class trial() functions return this; } void print() { // display function System.out.println("Inside the class B2"); } } class B3 extends B2 { // child class B3 inherits child class B2 @Override B3 trial() { // function with similar return type to that of the other child class trial() functions return this; } void print() { // display function System.out.println("Inside the class B3"); } } public class CovariantReturnTypeExample { // main class declaration public static void main(String[] args){ // main function declaration B1 b1 = new B1(); // parent class object creation b1.trial().print(); // parent class function calling B2 b2 = new B2(); // child class object creation b2.trial().print(); // child class function calling B3 b3 = new B3(); // child class object creation b3.trial().print(); // 2nd child class function calling } }

Output

Inside the class B1
Inside the class B2
Inside the class B3

In the above example, class B3 inherits class B2, which inherits class B1. Thus Class B1 is the parent class while B2 and B3 are child classes. So, any object of classes B2 and B3 is of the same type as that of class B1. In the function trial () present in all three classes B1, B2 and B3, one can notice that the return type is specific. So there is no confusion about knowing the return type of object from the method trial (). Even if there are more than 10 child classes, there will be no confusion regarding return type of all methods. This is possible with the help of covariant return type.

How can we Implement Covariant Return Type?

Return type based overloading is not accepted in Java. But it is accepted by the Java Virtual Machine (JVM). JVM uses full signature of any method for resolution, i.e. it includes argument and return types together. So, a class can have more than two methods with different return types. This fact is used by the javac to implement Covariant return types.

raja
Updated on 05-Sep-2022 09:58:26

Advertisements