Global Information Lookup Global Information

Covariant return type information


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:

class A {
}

class B extends A {
}

// "Class B is narrower than class A"
// Classes demonstrating method overriding:

class C {
    A getFoo() {
        return new A();
    }
}

class D extends C {
    // Overriding getFoo() in parent class C
    B getFoo() {
        return new B();
    }
}

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:

class Parent {
    public Object getFoo() {
        return null;
    }
}

class Child extends Parent {
    // String is child of the greater Object class
    public String getFoo() {
        return "This is a string";
    }

    // Driver code
    public static void main(String[] args) {
        Child child = new Child();
        System.out.println(child.getFoo());
    }
}
  1. ^ "Covariant Returns". Microsoft Docs. Retrieved 8 September 2021.
  2. ^ bridge Methods were introduced to circumvent problems introduced by polymorphism and the new generic type erasure

and 22 Related for: Covariant return type information

Request time (Page generated in 0.8399 seconds.)

Covariant return type

Last Update:

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...

Word Count : 332

Covariant derivative

Last Update:

mathematics, the covariant derivative is a way of specifying a derivative along tangent vectors of a manifold. Alternatively, the covariant derivative is...

Word Count : 6354

Type system

Last Update:

In some languages subtypes may also possess covariant or contravariant return types and argument types respectively. Certain languages, for example Clojure...

Word Count : 7253

Bottom type

Last Update:

or otherwise don't return normally, it's also used for covariant parameterized types. For example, Scala's List is a covariant type constructor, so List[Nothing]...

Word Count : 996

Object copying

Last Update:

type of the object being cloned instead due to Java's support for covariant return types. One advantage of using clone() is that since it is an overridable...

Word Count : 2171

Subtyping

Last Update:

parameter types, and B {\displaystyle {\mathtt {B}}} is its return type; "−" before the type means the type is contravariant while "+" means covariant. In languages...

Word Count : 3590

Generics in Java

Last Update:

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...

Word Count : 2789

Tensor

Last Update:

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...

Word Count : 9356

Java backporting tools

Last Update:

imports varargs enumerations reflection on generics and annotations covariant return types Retrolambda Yes Yes Retrotranslator No No Yes Yes Yes Yes Yes Yes...

Word Count : 139

Riemann curvature tensor

Last Update:

invariant of Riemannian metrics which measures the failure of the second covariant derivatives to commute. A Riemannian manifold has zero curvature if and...

Word Count : 2883

Dirac equation

Last Update:

and the entire probability 4-current density has the relativistically covariant expression J μ = i ℏ 2 m ( ψ ∗ ∂ μ ψ − ψ ∂ μ ψ ∗ ) . {\displaystyle J^{\mu...

Word Count : 13033

C Sharp syntax

Last Update:

their type parameters marked as covariant or contravariant, using keywords out and in, respectively. These declarations are then respected for type conversions...

Word Count : 10353

Comparison of C Sharp and Java

Last Update:

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...

Word Count : 13902

Light front quantization

Last Update:

treatment of spectroscopy and the parton structure of hadrons in a single covariant formalism, providing a unifying connection between low-energy and high-energy...

Word Count : 12723

Dark energy

Last Update:

theories that unifies dark matter and dark energy are suggested to be covariant theories of modified gravities. These theories alter the dynamics of spacetime...

Word Count : 9663

Loop quantum gravity

Last Update:

directions: the more traditional canonical loop quantum gravity, and the newer covariant loop quantum gravity, called spin foam theory. The most well-developed...

Word Count : 16373

Direct current

Last Update:

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...

Word Count : 1946

Feynman diagram

Last Update:

years earlier. Stueckelberg was motivated by the need for a manifestly covariant formalism for quantum field theory, but did not provide as automated a...

Word Count : 16026

Electrical network

Last Update:

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...

Word Count : 1238

DC motor

Last Update:

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...

Word Count : 2287

Social risk management

Last Update:

are referred to as covariant (or macro) shocks. Many more mechanisms are available for coping with idiosyncratic shocks than covariant shocks. The latter...

Word Count : 2149

Java collections framework

Last Update:

lang.Double. Collections are generic and hence invariant, but arrays are covariant. This can be considered an advantage of generic objects such as Collection...

Word Count : 4279

PDF Search Engine © AllGlobal.net