- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is covariant return type in Java?
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.
- Related Articles
- Covariant return type in Java\n
- What are covariant return types in Java?
- Covariant return types in Java
- Covariant return types in Java programming.
- What is the return type of a Constructor in Java?
- Importance of return type in Java?
- What is return type of db.collection.find() in MongoDB?
- What is the return type of a “count” query against MySQL using Java JDBC?
- What is type conversion in java?
- Does a constructor have a return type in Java?
- Can we change return type of main() method in java?
- What is a Type-safe Enum in Java?\n
- Implicit return type int in C
- If I change the return type, will the method gets overloaded in java?
- What is a target type of lambda expression in Java?
