Covariance and contravariance (computer science): Difference between revisions

Content deleted Content added
Function types: Fixed unmatched parentheses
RnTkm (talk | contribs)
Undid revision 1054485302 by RnTkm (talk). withdraw
Line 110:
 
== Inheritance in object-oriented languages ==
When a subclass [[Method overriding|overrides]] a method in a superclass, the compiler must check that the overriding method has the right type. While some languages require that the type exactly matches the type in the superclass (invariance{{Dubious|date=November 2021|reason=according to the definition above, 'invariant' means noncompatible, it doesn't mean "the same type" (bidirectionally compatible).}}), it is also type safe to allow the overriding method to have a "better" type. By the usual subtyping rule for function types, this means that the overriding method should return a more specific type (return type covariance), and accept a more general argument (parameter type contravariance). In [[Unified Modeling Language|UML]] notation, the possibilities are as follows:
 
<gallery perrow="5" heights="190" caption="Variance and method overriding: overview">