Sling Academy
Home/Kotlin/How to Define Generic Classes in Kotlin

How to Define Generic Classes in Kotlin

Last updated: November 30, 2024

Kotlin, a modern programming language for JVM, allows you to write concise and expressive code. One of its powerful features is generics, which enable you to define classes and functions with a placeholder for type. This article will guide you through the process of defining generic classes in Kotlin with practical examples.

Understanding Generics

Generics help you create classes, methods, and interfaces with type parameters. This functionality enhances code reusability and safety by eliminating the need for casting and helps you avoid class cast exceptions during runtime.

Creating a Basic Generic Class

Let’s start by defining a simple generic class in Kotlin.

class Box<T>(var content: T) {
    fun replaceContent(newContent: T) {
        content = newContent
    }
}

In the code above:

  • T is a type parameter. It can be replaced by any type when you instantiate the class.
  • content is a variable of the type T, meaning it can hold any data type.
  • replaceContent is a function that allows you to change the content to a new value of the same type.

Instantiating Generic Classes

To create an instance of a generic class, provide the type as a parameter:

fun main() {
    val intBox: Box<Int> = Box(123)
    println(intBox.content)  // Prints: 123
    intBox.replaceContent(456)
    println(intBox.content)  // Prints: 456
    
    val stringBox = Box("Hello")
    println(stringBox.content)  // Prints: Hello
    stringBox.replaceContent("World")
    println(stringBox.content)  // Prints: World
}

Here:

  • Box<Int> creates a Box that holds integer values.
  • Box("Hello") creates a Box capable of holding String.

Generic Constraints

Sometimes, you may want to limit the types that can be used as arguments for a generic type. This is achieved through constraints. For instance, you can specify that a type must inherit from a specific superclass or implement an interface.

class NumberBox<T : Number>(val number: T)

fun main() {
    val intBox = NumberBox(10)      // Works fine
    val doubleBox = NumberBox(10.5) // Works fine
    // val stringBox = NumberBox("Hello") // Compilation error
}

In this example, T is constrained to be a subtype of Number.

Variance in Generics

Kotlin's generics support both covariant and contravariant generic types, helping you manage flexibility when dealing with type parameters.

Covariance allows you to use a subtype as the actual type:

// Covariant
class Covariant<out T>(val value: T)

fun main() {
    val strCovariant: Covariant<String> = Covariant("Hello")
    val anyCovariant: Covariant<Any> = strCovariant // OK
    println(anyCovariant.value)
}

Contravariance allows you to use a supertype as the actual type:

// Contravariant
class Contravariant<in T>

fun main() {
    val strContravariant: Contravariant<String> = Contravariant()
    val anyContravariant: Contravariant<Any> = strContravariant // OK
}

Conclusion

Generics in Kotlin offer a robust way to write type-safe and reusable code. From defining basic generic classes to applying constraints and variance, mastering these techniques will empower you to write cleaner Kotlin code. Practice defining and using generics in your applications to fully harness their potential.

Next Article: Creating Generic Functions in Kotlin

Previous Article: Introduction to Generics in Kotlin

Series: Advanced Kotlin Features

Kotlin

You May Also Like

  • How to Use Modulo for Cyclic Arithmetic in Kotlin
  • Kotlin: Infinite Loop Detected in Code
  • Fixing Kotlin Error: Index Out of Bounds in List Access
  • Setting Up JDBC in a Kotlin Application
  • Creating a File Explorer App with Kotlin
  • How to Work with APIs in Kotlin
  • What is the `when` Expression in Kotlin?
  • Writing a Script to Rename Multiple Files Programmatically in Kotlin
  • Using Safe Calls (`?.`) to Avoid NullPointerExceptions in Kotlin
  • Chaining Safe Calls for Complex Operations in Kotlin
  • Using the Elvis Operator for Default Values in Kotlin
  • Combining Safe Calls and the Elvis Operator in Kotlin
  • When to Avoid the Null Assertion Operator (`!!`) in Kotlin
  • How to Check for Null Values with `if` Statements in Kotlin
  • Using `let` with Nullable Variables for Scoped Operations in Kotlin
  • Kotlin: How to Handle Nulls in Function Parameters
  • Returning Nullable Values from Functions in Kotlin
  • Safely Accessing Properties of Nullable Objects in Kotlin
  • How to Use `is` for Nullable Type Checking in Kotlin