Variance, Covariance, Contravariance
Type(T)
composite data type - is a type which is built out of another type. For example it can be generics with a wildcard, containers, function types...[example]
method's types - parameters value and returned value and theit types
Variance
Variance - is about assignment compatibility. It is an ability to use a derived type instead of original type. It is not parent-child relationship
X(T) - composite data type or method's types X, with type T
Covariance you are able to assign more derived type than original type
X(T) covariant or X(T1) is covariant to X(T2) when relation T1 to T2 is the same as X(T1) to X(T2)
Contravariance you are able to assign less derived type then original type
X(T) contravariant or X(T1) is contravariant to X(T2) when relation T1 to T2 is the same as X(T2) to X(T1)
Invariance neither Covariance not Contravariance
Examples
class A { }
//B is A
class B extends A { }
Reference type Array in Java is covariant
A[] aArray = new A[2];
B[] bArray = new B[2];
//B[] is covariant to A[] because
aArray = bArray;
class Generic { }
//A - original type, B - more derived type
//Generic is covariant to Generic
Generic extends A> ref = new Generic(); //covariant
//B - original type, A - less derived type
//Generic is contravariant to Generic
Generic super B> ref = new Generic(); //contravariant
[Liskov principle]
[Java generics]