What Is Covariance

I think this is more clearly explained in ContraVsCoVariance. I would recommend this material - where relevant - should be moved there and references to this page adjusted accordingly.


In an overridden method, an argument or return value made more specialized is covariant (the type is replaced by a descendant of the original type)

An argument or return value made more general is contravariant (?)

See CovariantReturnTypes.

Languages with full support for Covariance:


I remember all of the covariant (EiffelLanguage) v. novariant (CeePlusPlus, JavaLanguage) v. contravariant (SatherLanguage) debates. At this point, I don't think many people care any more. I have never missed covariance in argument types, but have often missed it in return types. Covariant argument types are not type safe (despite the best efforts of the Eiffel folks), and seem pointless to me, though I've been doing objects for many years now. Covariant return types are safe, and sometimes convenient. --KevinLewis


For what it is worth, this notation probably originally comes from general tensor theory, applicable to inner product space representations. It turns out that the most convenient formulation for dealing with the vectors implies two representations of each vector; the first is called the covariant representation, and the second the contravariant. The names come from relating transformations of these representations to mappings between basis vectors. Covariant vectors transform "in the same way" as basis vectors, while Contravariant vectors transform "in the opposite way"...


EditText of this page (last edited August 16, 2010) or FindPage with title or text search