In object-oriented programming, a covariant return type of a method is one that can be replaced by a "narrower" (derived) type when the method is overridden in a subclass. A notable language in which this is a fairly common paradigm is C++.
C# supports return type covariance as of version 9.0.[1] Covariant return types have been (partially) allowed in the Java language since the release of JDK5.0,[2] so the following example wouldn't compile on a previous release:
// Classes used as return types:classA{}classBextendsA{}// "Class B is narrower than class A"// Classes demonstrating method overriding:classC{AgetFoo(){returnnewA();}}classDextendsC{// Overriding getFoo() in parent class CBgetFoo(){returnnewB();}}
More specifically, covariant (wide to narrower) or contravariant (narrow to wider) 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. If substitution is not allowed, the return type is invariant and causes a compile error.
Another example of covariance with the help of built in Object and String class of Java:
classParent{publicObjectgetFoo(){returnnull;}}classChildextendsParent{// String is child of the greater Object classpublicStringgetFoo(){return"This is a string";}// Driver codepublicstaticvoidmain(String[]args){Childchild=newChild();System.out.println(child.getFoo());}}
^"Covariant Returns". Microsoft Docs. Retrieved 8 September 2021.
^bridge Methods were introduced to circumvent problems introduced by polymorphism and the new generic type erasure
and 22 Related for: Covariant return type information
object-oriented programming, a covariantreturntype of a method is one that can be replaced by a "narrower" (derived) type when the method is overridden...
mathematics, the covariant derivative is a way of specifying a derivative along tangent vectors of a manifold. Alternatively, the covariant derivative is...
In some languages subtypes may also possess covariant or contravariant returntypes and argument types respectively. Certain languages, for example Clojure...
or otherwise don't return normally, it's also used for covariant parameterized types. For example, Scala's List is a covarianttype constructor, so List[Nothing]...
type of the object being cloned instead due to Java's support for covariantreturntypes. One advantage of using clone() is that since it is an overridable...
parameter types, and B {\displaystyle {\mathtt {B}}} is its returntype; "−" before the type means the type is contravariant while "+" means covariant. In languages...
public static <Type> Entry<Type, Type> twice(Type value) { return new Entry<Type, Type>(value, value); } Note: If we remove the first <Type> in the above...
matrix of a linear operator has one covariant and one contravariant index: it is of type (1,1). Combinations of covariant and contravariant components with...
invariant of Riemannian metrics which measures the failure of the second covariant derivatives to commute. A Riemannian manifold has zero curvature if and...
and the entire probability 4-current density has the relativistically covariant expression J μ = i ℏ 2 m ( ψ ∗ ∂ μ ψ − ψ ∂ μ ψ ∗ ) . {\displaystyle J^{\mu...
their type parameters marked as covariant or contravariant, using keywords out and in, respectively. These declarations are then respected for type conversions...
root type. Consequently, all types implement the methods of this root type, and extension methods defined for the object type apply to all types, even...
treatment of spectroscopy and the parton structure of hadrons in a single covariant formalism, providing a unifying connection between low-energy and high-energy...
theories that unifies dark matter and dark energy are suggested to be covariant theories of modified gravities. These theories alter the dynamics of spacetime...
directions: the more traditional canonical loop quantum gravity, and the newer covariant loop quantum gravity, called spin foam theory. The most well-developed...
distinguishing it from alternating current (AC). A term formerly used for this type of current was galvanic current. The abbreviations AC and DC are often used...
years earlier. Stueckelberg was motivated by the need for a manifestly covariant formalism for quantum field theory, but did not provide as automated a...
An electrical circuit is a network consisting of a closed loop, giving a return path for the current. Thus all circuits are networks, but not all networks...
mechanical force. The most common types rely on magnetic forces produced by currents in the coils. Nearly all types of DC motors have some internal mechanism...
are referred to as covariant (or macro) shocks. Many more mechanisms are available for coping with idiosyncratic shocks than covariant shocks. The latter...
lang.Double. Collections are generic and hence invariant, but arrays are covariant. This can be considered an advantage of generic objects such as Collection...