marcinmoskala
10/13/2017, 7:04 AMclass 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).