Why isn't it possible to pass an object of the typ...
# announcements
j
Why isn't it possible to pass an object of the type 
ZusammenfassungABC
 , even if it inherits from 
ZusammenfassungAC
 (line 22)?
The warning message IntelliJ gives:
d
Generics are invariant by default. The docs explain this: https://kotlinlang.org/docs/reference/generics.html
☝️ 1
z
Add the right
out
and
in
modifiers and it should work I think.
👍 2
j
So, this is the "fix"?
n
Yes though you want to make sure that the
in
and
out
modifiers are actually appropriate
Sometimes in Kotlin, given the way inheritance work, there's no alternative except to simply duplicate the code
t
the best fix would probably be this:
Copy code
open class ErstTyp<out T : GrundTyp>
This out-projection allows the compiler to use instance with a more specific generic type to be used in places where a superclass would be expected as the generic parameter.
👍 1
it basically means you can safely cast
ErstTyp<T>
to
ErstTyp<U>
if
U
inherits from
T
And refering to your original question: you're not passing an object of type
ZusammenfassungABC
, but an object of type
ErstTyp<ZusammenfassungABC>
. That's an important difference.
j
Thank you for adding that!