A Kotlin generic class is a class parameterized over types, defined by declaring one or more formal type parameters within angle brackets (Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
<>) immediately following the class name. This mechanism enables the creation of type-safe data structures by deferring the specification of exact concrete types until the class is instantiated, allowing the compiler to enforce type constraints statically.
Basic Syntax and Instantiation
Type parameters are conventionally named with single uppercase letters (e.g.,T for Type, K for Key, V for Value). These parameters can be used as property types, return types, or function parameter types within the class body.
Declaration-Site Variance
Kotlin utilizes declaration-site variance to define subtyping rules for generic classes using theout and in modifiers. By default, generic classes in Kotlin are invariant, meaning Container<String> is not a subtype of Container<Any>.
Covariance (out)
The out modifier marks a type parameter as covariant. It restricts the type parameter to only be produced (returned from functions or exposed as read-only properties). If Derived is a subtype of Base, then Producer<Derived> is a subtype of Producer<Base>.
Contravariance (in)
The in modifier marks a type parameter as contravariant. It restricts the type parameter to only be consumed (passed as arguments to functions). If Derived is a subtype of Base, then Consumer<Base> is a subtype of Consumer<Derived>.
Type Constraints (Bounds)
Type constraints restrict the set of types that can be substituted for a given type parameter.Single Upper Bound
The most common constraint is an upper bound, denoted by a colon (:). The default upper bound if none is specified is Any?.
Multiple Upper Bounds
If a type parameter must satisfy multiple constraints, Kotlin requires awhere clause at the end of the class declaration.
Type Erasure and Star-Projections
Kotlin generics are subject to type erasure at runtime. The compiler ensures type safety, but the generic type information is not retained in the compiled bytecode. For example, bothContainer<Int> and Container<String> are erased to Container.
When the specific type argument is unknown or irrelevant, but the class must still be used safely, Kotlin provides star-projections (*).
Master Kotlin with Deep Grasping Methodology!Learn More





