Generics & Variance
KOTLIN › Types & Classes
Kotlin generic types, in/out variance, projections, erasure, and reified type parameters.
Variance and erasure are classic Kotlin interview filters: they reveal whether you understand subtyping rules versus just syntax. Expect to explain why a List<Dog> can stand in for a List<Animal>, when the compiler rejects a producer/consumer position, and why reified only exists inside inline functions. Interviewers probe declaration-site versus use-site variance, star projection meaning, and what survives type erasure at runtime.
What this covers
- Declaring generic classes/functions and constraints (upper bounds, where clauses)
- Declaration-site variance: out = covariant producer, in = contravariant consumer
- Use-site variance (type projections) and star projection semantics
- Type erasure: what generic info disappears at runtime and the consequences
- Reified type parameters, why they require inline, and what they enable
- Distinguishing Kotlin's declaration-site model from Java's use-site wildcards
Study this topic
- Generics & Variance explained: the guided lesson
- 12 practice quiz questions
- 9 revision flashcards
- Generics & Variance interview questions and answers