Covariant return type
Encyclopedia
In object-oriented programming
, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden
in a subclass.
Covariant return types have been (partially) allowed in the Java language
since the release of JDK5.0, so the following example wouldn't compile on a previous release:
More specifically, covariance of the return type refers to a situation where the return type of the overriding method is changed to a type related to (but different from) the return type of the original overridden method. The relationship between the two covariant return types is usually one which allows substitution of the one type with the other, following the Liskov substitution principle
. This usually implies that the return types of the overriding methods will be subtypes of the return type of the overridden method. The above example specifically illustrates such a case.
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, a covariant return type of a method is one that can be replaced by a "narrower" type when the method is overridden
Method overriding (programming)
Method overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes...
in a subclass.
Covariant return types have been (partially) allowed in the Java language
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
since the release of JDK5.0, so the following example wouldn't compile on a previous release:
More specifically, covariance of the return type refers to a situation where the return type of the overriding method is changed to a type related to (but different from) the return type of the original overridden method. The relationship between the two covariant return types is usually one which allows substitution of the one type with the other, following the Liskov substitution principle
Liskov substitution principle
Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program...
. This usually implies that the return types of the overriding methods will be subtypes of the return type of the overridden method. The above example specifically illustrates such a case.
See also
- Covariance and contravariance (computer science)Covariance and contravariance (computer science)Within the type system of a programming language, covariance and contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations ....
- Covariant return types in C++ Example
- Java project which takes advantage of covariant return types in Java