Variance in Java
In other words, methods with return values of the type parameter are accessible, while methods with input arguments of the type parameter are inaccessible. Contravariant containers behave counterintuitively: contrary to covariant containers, access to methods with return values of the type parameter are inaccessible while methods with input arguments of the type parameter are accessible:
You can exchange the subtype for the supertype:
Cannot capture a specific type when reading from them:
You can add subtypes of the “lower bound”:
But you cannot add supertypes:
The use-site must declare an unbounded wildcard on the type parameter. Same principles apply, you just have more type parameters to consider:
Java allows overriding methods with covariant return types and exception types:
But attempting to override methods with covariant arguments results in merely an overload:
Variance introduces additional complexity to Java.
Source: llorllale.github.io