

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 type in Java
Covariant return type refers to return type of an overriding method. It allows to narrow down return type of an overridden method without any need to cast the type or check the return type. Covariant return type works only for non-primitive return types.
From Java 5 onwards, we can override a method by changing its return type only by abiding the condition that return type is a subclass of that of overridden method return type.
Example
Following example showcases the same.
class SuperClass { SuperClass get() { System.out.println("SuperClass"); return this; } } public class Tester extends SuperClass { Tester get() { System.out.println("SubClass"); return this; } public static void main(String[] args) { SuperClass tester = new Tester(); tester.get(); } }
Output
Subclass
- Related Questions & Answers
- What is covariant return type in Java?
- Covariant return types in Java
- Covariant return types in Java programming.
- What are covariant return types in Java?
- Importance of return type in Java?
- Does a constructor have a return type in Java?
- Can we change return type of main() method in java?
- What is the return type of a Constructor in Java?
- Implicit return type int in C
- Function overloading and return type in C++
- Can we overload a method based on different return type but same argument type and number, in java?
- If I change the return type, will the method gets overloaded in java?
- What is return type of db.collection.find() in MongoDB?
- Boolean Type in Java
- Type Erasure in Java
Advertisements