Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

generics-kotlin

Generics

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Generics: the syntax, patterns, and safety habits you need before advancing in Kotlin.

Teams still ship Generics in Kotlin codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Generics in contexts like: Android apps, Spring services, and shared KMP modules.

Write Kotlin in main.kt with fun main(), click Run on server—the dev runner kotlinc compiles to a JVM jar and java runs it; use println for output (requires JDK + kotlinc; LEARNING_RUNNER_ENABLED=true).

When you can explain the previous lesson's ideas without copying starter code.

Kotlin generics resemble Java and C# type parameters but add declaration-site variance (out, in) and reified types with inline functions.

Variance

interface Producer<out T> {
    fun produce(): T
}

out T is covariant (producer); in T is contravariant (consumer). Use where Java wildcards feel awkward.

Constraints and star projection

fun <T : Comparable<T>> sort(list: List<T>) bounds types. List<*> is an unknown element type—read-only for out, write caveats for in.

Important interview questions and answers

  1. Q: out vs in?
    A: out means you only return T (covariant); in means you only consume T (contravariant).
  2. Q: Reified type parameters?
    A: Available with inline functions—type info preserved at runtime for T::class checks in the same function.

Self-check

  1. Which variance keyword marks a producer interface?
  2. What does List<*> mean?

Tip: Use out for producers and in for consumers—Pecs (producer extends, consumer super) in Kotlin form.

Interview prep

out vs in?

out producer (read), in consumer (write parameters).

reified?

Inline generics retain type at runtime in inlined functions.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • in/out variance?
  • reified types?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump