
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Covariant return types in Java
In Java, covariant return types allow an overriding method to return a subtype of the supertype. Here, the return type of the parent class is called the supertype, and the return type of the child class is known as the subtype, if and only if it is a subclass of the parent class's return type. Sounds confusing, right? Well! It is not as confusing as it sounds. Read the whole article to understand this concept.
Java Covariant Return Types
Covariant return type refers to the return type of an overriding method. It works only for non-primitive return types, such as Classes, Arrays, Interfaces, and so on. The whole idea of the covariant return type is based on the Liskov substitution principle, which states that if a class, let's say a class named One, is a subtype of another class Two, then class Two can be replaced with One without affecting the behavior of the program.
As we know, if a subclass provides a specific implementation of a method that is already provided by its parent class, it is called Method Overriding. In it, the method name and parameters must be the same. However, the return type was also the same before the release of Java version 5.
With the release of Java 5, a covariant return type was introduced, which allows us to override methods having different return types. But there are certain guidelines you need to follow.
Rules for Java Covariant Return Types
You should follow the rules given below for covariant return type in Java:
- The return types should not be primitive types.
- We can override a method by changing its return type, but you must abide by the condition that the return type should be a subclass of that of overridden method return type. For example, if return type of parent class is Object, then return type of child class can be String as it is a subclass of Object.
Uses of Covariant Return Type
The list given below shows some use cases of covariant return type in Java:
- It allows to narrow down return type of an overridden method without any need to cast the type or check the return type.
- You can avoid ClassCastExceptions.
- It also minimize the use of upcasting and downcasting feature of Java.
Example of Covariant Return Type
To put Covariant return types in practice, let's take a simple Java program where we will define a parent and child class. The child class will override the method of parent class with different return type.
class Cricket { Cricket getType() { System.out.println("This is an International sport"); return this; } } class Ipl extends Cricket { @Override Ipl getType() { System.out.println("IPL: This is an Indian domestic sports league"); return this; } } public class Sports { public static void main(String[] args) { Cricket sportLeague = new Ipl(); sportLeague.getType(); } }
The output of the above code is as follows:
IPL: This is an Indian domestic sports league