This refers to how the subtype relationship between complex types relates to the subtype relationship between its components.
A complex type is a nested data structure with primitive data types or other complex types. With regards to complex type, if:
, it is covariant , it is contravariant - otherwise, it is invariant
Arrays in Java are covariant.
Given an array
S[] sArray = ...
, andS <: T
, we can doT[] tArray = sArray
.
However, this causes issues, as if we do this, we can then put items of T
that may not be subtypes of S
into the array which is still of run-time type S[]
. Thus, type safety can be compromised.