Generic Classes
In this page:
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
class Box<T>(val item: T)
fun main() {
val intBox = Box(42)
val stringBox = Box("Kotlin")
println("${intBox.item}, ${stringBox.item}")
}
Login to try C/C++/Java/PHP code in the editor
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
class Pair2<A, B>(val first: A, val second: B)
fun main() {
val pair = Pair2("age", 30)
println("${pair.first} = ${pair.second}")
}
Login to try C/C++/Java/PHP code in the editor
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
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())
}
Login to try C/C++/Java/PHP code in the editor
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
class KeyValue<K, V>(val key: K, val value: V) {
fun describe() = "$key -> $value"
}
fun main() {
val entry = KeyValue("name", "Kotlin")
println(entry.describe())
}
Login to try C/C++/Java/PHP code in the editor
- Writing a separate class for every type it needs to hold (e.g.
IntBox,StringBox) instead of one genericBox<T>. - Forgetting to specify the type argument when it cannot be inferred, such as creating an empty generic collection with no elements to infer from.
- Confusing the type parameter name (like
T) with an actual type;Tis just a placeholder name, conventionally a single capital letter.
- 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
Tacts 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: