0Pricing
Kotlin Academy · Lesson

Generic Functions and Classes

Parameterize types.

Generic Functions and Classes is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Are Generics?

Generics let you write code that works with many types while keeping full type safety. Instead of fixing a concrete type, you use a type parameter (often T) as a placeholder.

A Generic Function

A generic function declares its type parameter in angle brackets before the function name. Here T can be any type.

fun <T> identity(value: T): T {
    return value
}

fun main() {
    println(identity(42))
    println(identity("hello"))
}

Type Inference

You rarely specify the type explicitly; the compiler infers T from the arguments. You can still write it as identity<Int>(42) if needed.

fun <T> firstOf(a: T, b: T): T = a

fun main() {
    val x = firstOf(10, 20)
    val y = firstOf("a", "b")
    println("$x $y")
}

Multiple Type Parameters

A function can have several type parameters, useful for working with pairs of unrelated types.

fun <A, B> makePair(first: A, second: B): Pair<A, B> {
    return Pair(first, second)
}

fun main() {
    val p = makePair("age", 30)
    println(p)
}

A Generic Class

Classes can be generic too. The type parameter is declared after the class name and can be used for properties and functions.

class Box<T>(val value: T) {
    fun get(): T = value
}

fun main() {
    val intBox = Box(123)
    val strBox = Box("Kotlin")
    println(intBox.get())
    println(strBox.get())
}

Generic Class with Behavior

Generic classes can hold mutable state and operate on the type parameter, like a simple container.

class Stack<T> {
    private val items = mutableListOf<T>()
    fun push(item: T) { items.add(item) }
    fun pop(): T = items.removeAt(items.size - 1)
    fun isEmpty() = items.isEmpty()
}

fun main() {
    val stack = Stack<Int>()
    stack.push(1)
    stack.push(2)
    println(stack.pop())
}

Generic Interfaces

Interfaces can declare type parameters too, letting implementations choose the concrete type.

interface Container<T> {
    fun item(): T
}

class StringContainer : Container<String> {
    override fun item(): String = "fixed"
}

fun main() {
    println(StringContainer().item())
}

Why Not Just Use Any?

You could store values as Any, but you would lose type information and need casts. Generics preserve the exact type so the compiler catches mistakes and no casting is needed.

fun main() {
    val box: Box<String> = Box("text")
    val s: String = box.get()
    println(s.uppercase())
}

class Box<T>(val value: T) { fun get(): T = value }

Generic Extension Functions

Extension functions can be generic, allowing reusable utilities across all types.

fun <T> T.printAndReturn(): T {
    println("Value: $this")
    return this
}

fun main() {
    val x = 5.printAndReturn()
    "hi".printAndReturn()
    println(x)
}

Naming Conventions

By convention, single uppercase letters name type parameters: T for a generic type, E for an element, K and V for map keys and values, R for a result.

Generics Everywhere

Kotlin's standard library is built on generics: List<T>, Map<K, V>, Pair<A, B>. Understanding generics helps you read and use these types confidently.

Quick Check

What is the main benefit of generics over using Any?

Recap

Generics use type parameters like T to write reusable, type-safe functions, classes, and interfaces. The compiler infers types and enforces safety, avoiding casts. Next you will control how generic types relate using declaration-site variance with in and out.

Frequently asked questions

Is the “Generic Functions and Classes” lesson free?

Yes — the full text of “Generic Functions and Classes” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Functions and Classes”?

Parameterize types. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Functions and Classes” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic Functions and Classes
  2. Declaration-Site Variance
  3. Use-Site Variance
  4. Generic Constraints
← Back to Kotlin Academy