General idea it that if A is supertype of B, (B i...
# android
m
General idea it that if A is supertype of B, (B is subtype of A) When
class Box<T>
=> no relation between
Box<A>
and
Box<B>
, Box is invariant When
class Box<out T>
=>
Box<A>
is supertype of
Box<B>
, Box is covariant (
Box<B>
is subtype of
Box<A>
) When
class Box<in T>
=>
Box<B>
is supertype of
Box<A>
, Box is conravariant (
Box<A>
is subtype of
Box<B>
) If you want to accept Box with any subtype then use
Box<*>
But the subject is more expanded. The reasons behind it and why it is important were described by @igor.wojda in Android Development with Kotlin in Generic chapter. There were also generic declaration sites, available positions for type parameters (and the reasons behind it) and much more. Even though the chapter is not so long (it is about 30 pages length).