Covariance and contravariance (computer science): Difference between revisions

Content deleted Content added
m Data: Cleanup and typo fixing, typo(s) fixed: similarly- → similarly
Inheritance in object-oriented languages: Added helpful description, to avoid having to look up UML.
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), 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 (where Class B extends Class A):
 
<gallery perrow="5" heights="190" caption="Variance and method overriding: overview">