Skip to main content
InterviewHubPRO
Section Overview
Q44

Question 44 of 50

Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

88%
Kotlin & Coroutines (50 Qs)•Part 3: K2 Compiler, KMP, Advanced Metaprogramming & Optimization (25 - 35)

Type Projections & Variance: `out` covariant vs `in` contravariant.

senior LevelPerformance

In Simple Words:

  • Covariance (out T / Producer): Allows subtyping: List<Dog> is a subtype of List<Animal>.
    • Can only produce/read T; cannot accept T as a method parameter.
  • Contravariance (in T / Consumer): Inverses subtyping: Comparable<Animal> can be used where Comparable<Dog> is expected.
    • Can only accept/write T; cannot return T.
  • PECS Rule: Producer-Extends (out), Consumer-Super (in).