
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Covariant return types 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.
Following example showcases the same.
Example
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 Articles
- Covariant return types in Java programming.
- What are covariant return types in Java?
- Covariant return type in Java\n
- What is covariant return type in Java?
- Data types in Java
- Types of variable in java
- Types of inheritance in Java
- Types of packages in Java
- Types of Array in Java
- Types of References in Java
- Bounded-types in generics in Java?
- Return all system properties in Java
- Importance of return type in Java?
- Types of access modifiers in Java?
- Types of quantifiers in Java regex

Advertisements