0
0
Kotlinprogramming~10 mins

Generic class declaration in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Generic class declaration
Start
Declare generic class with <T>
Create instance with specific type
Use instance with that type
End
This flow shows how a generic class is declared with a type parameter and then used with a specific type.
Execution Sample
Kotlin
class Box<T>(val value: T) {
    fun getValue(): T = value
}

val intBox = Box(123)
println(intBox.getValue())
Defines a generic class Box that holds a value of any type T, then creates an instance with an Int and prints the value.
Execution Table
StepActionType Parameter TInstance CreatedOutput
1Declare class Box<T>T is a placeholderNo instance yet
2Create intBox = Box(123)T = IntintBox of type Box<Int>
3Call intBox.getValue()T = IntintBox123
4Print output123
5EndExecution stops
💡 No more code to execute, program ends
Variable Tracker
VariableStartAfter Step 2After Step 3Final
TplaceholderIntIntInt
intBoxnoneBox<Int>(123)Box<Int>(123)Box<Int>(123)
Outputnonenone123123
Key Moments - 3 Insights
Why do we write <T> after the class name?
The <T> tells Kotlin this class uses a generic type placeholder. See execution_table step 1 where T is a placeholder.
How does Kotlin know what type T is?
When we create an instance like Box(123), Kotlin sets T to Int automatically. See execution_table step 2.
What type does getValue() return?
It returns the same type T that was used when creating the instance. Here, getValue() returns Int as in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the type parameter T when intBox is created?
AAny
BString
CInt
DNo type assigned
💡 Hint
Check execution_table row 2 under 'Type Parameter T'
At which step does the program print the value 123?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at execution_table row 4 under 'Output'
If we create Box("hello") instead of Box(123), what changes in variable_tracker?
AT stays Int, value changes to String
BT changes to String, intBox value changes
CNo changes, T is always Int
DProgram will not compile
💡 Hint
Generic type T adapts to the instance type, see variable_tracker row for T
Concept Snapshot
Generic class declaration syntax:
class ClassName<T>(val value: T) { ... }

- T is a placeholder type
- When creating instance, specify actual type
- Methods use T as the type
- Enables reusable, type-safe classes
Full Transcript
This example shows how to declare a generic class in Kotlin using a type parameter T. The class Box holds a value of type T. When we create an instance like Box(123), Kotlin sets T to Int automatically. Calling getValue() returns the stored value with the correct type. The execution table traces each step: declaring the class, creating an instance, calling the method, and printing the output. The variable tracker shows how T and variables change during execution. Key moments clarify why <T> is used, how Kotlin infers the type, and what getValue() returns. The quiz tests understanding of type assignment, output timing, and how changing the instance type affects variables.