IntermediateGenerics

Generic Class

Create a class that works with any type.

kotlin
class Box<T>(val value: T)

fun main() {
    val stringBox = Box("Kotlin")
    val intBox = Box(100)
    println(stringBox.value)
    println(intBox.value)
}
Output
Kotlin 100

Explanation

<T> is a type parameter. Box can hold any type. The compiler infers T from the constructor argument.