Covariant return type

Covariant return type

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:

 // Classes used as return types:
 
 class A {
 }
 
 class B extends A {
 }
 
 // Classes demonstrating method overriding:
 
 class C {
     A getFoo() {
         return new A();
     }
 }
 
 class D extends C {
     B getFoo() {
         return new B();
     }
 }

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.

See also


Wikimedia Foundation. 2010.

Игры ⚽ Нужно решить контрольную?

Look at other dictionaries:

  • Covariant derivative — In mathematics, the covariant derivative is a way of specifying a derivative along tangent vectors of a manifold. Alternatively, the covariant derivative is a way of introducing and working with a connection on a manifold by means of a… …   Wikipedia

  • Bottom type — In type theory, the bottom type is the subtype of all types. It is the return type of a function that does not return normally. As an example, a certain program function may promise to return an integer but in a Turing complete language the… …   Wikipedia

  • 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 (such as parameters, generics, and return… …   Wikipedia

  • clone (Java method) — clone() is a method in the Java programming language for object duplication. In Java, objects are manipulated through reference variables, and there is no operator for copying an object the assignment operator duplicates the reference, not the… …   Wikipedia

  • C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… …   Wikipedia

  • Comparison of C Sharp and Java — The correct title of this article is Comparison of C# and Java. The substitution or omission of the # sign is because of technical restrictions. Programming language comparisons General comparison Basic syntax Basic instructions …   Wikipedia

  • Albert Einstein — Einstein redirects here. For other uses, see Einstein (disambiguation) …   Wikipedia

  • Clifford analysis — Clifford analysis, using Clifford algebras named after William Kingdon Clifford, is the study of Dirac operators, and Dirac type operators in analysis and geometry, together with their applications. Examples of Dirac type operators include, but… …   Wikipedia

  • Dark energy — Not to be confused with dark matter, dark fluid, or dark flow. Physical cosmology …   Wikipedia

  • Magnetism — This article is about magnetic materials. For information about objects and devices that produce a magnetic field, see magnet. For fields that magnets and currents produce, see magnetic field. For other uses, see magnetism (disambiguation).… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”