← Back to Kotlin Course | Chapter 11: Generics | Lesson 1 of 6

Generic Classes

A generic class is a container blueprint that leaves a blank for 'what type of thing goes inside', to be filled in later.

Declaring a Generic Class

A type parameter in angle brackets, like <T>, after the class name lets that class work with any type, filled in as T wherever it's used inside.

Example: Declaring a Generic Class

markup
class Box<T>(val item: T)

fun main() {
    val intBox = Box(42)
    val stringBox = Box("Kotlin")
    println("${intBox.item}, ${stringBox.item}")
}

Type Inference for Generics

Kotlin usually infers the type argument automatically from the value passed to the constructor, so you rarely need to write Box<Int> explicitly.

Example: Type Inference for Generics

markup
class Pair2<A, B>(val first: A, val second: B)

fun main() {
    val pair = Pair2("age", 30)
    println("${pair.first} = ${pair.second}")
}

Explicit Type Arguments

When there is nothing for the compiler to infer from, such as creating an empty generic container, the type argument must be provided explicitly in angle brackets.

Example: Explicit Type Arguments

markup
class Container<T> {
    private val items = mutableListOf<T>()
    fun add(item: T) = items.add(item)
    fun all(): List<T> = items
}

fun main() {
    val container = Container<String>()
    container.add("Kotlin")
    container.add("Generics")
    println(container.all())
}

Multiple Type Parameters

A generic class can declare more than one type parameter, letting it work with several independent placeholder types at once.

Example: Multiple Type Parameters

markup
class KeyValue<K, V>(val key: K, val value: V) {
    fun describe() = "$key -> $value"
}

fun main() {
    val entry = KeyValue("name", "Kotlin")
    println(entry.describe())
}
Common Mistakes
  1. Writing a separate class for every type it needs to hold (e.g. IntBox, StringBox) instead of one generic Box<T>.
  2. Forgetting to specify the type argument when it cannot be inferred, such as creating an empty generic collection with no elements to infer from.
  3. Confusing the type parameter name (like T) with an actual type; T is just a placeholder name, conventionally a single capital letter.
Chapter Summary
  • A generic class declares one or more type parameters in angle brackets after its name, such as class Box<T>(val item: T).
  • The type parameter T acts as a placeholder that gets replaced with a concrete type when the class is used.
  • Type arguments are often inferred automatically from constructor arguments, but can be specified explicitly when needed.
  • Generic classes avoid duplicating the same logic for every different type it might need to hold.
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.