What if you could write one class that magically fits any type you want?
Why Generic class declaration in Kotlin? - Purpose & Use Cases
Imagine you want to create a box that can hold any type of item--apples, books, or toys. Without generics, you'd have to make a new box class for each item type, like AppleBox, BookBox, ToyBox, and so on.
This manual way is slow and boring. Every time you want a new box for a different item, you write almost the same code again. It's easy to make mistakes and hard to keep track of many similar classes.
Generic class declaration lets you create one flexible box that works for any item type. You write the box code once, and it adapts to hold apples, books, or toys without extra work.
class AppleBox(val item: Apple) class BookBox(val item: Book)
class Box<T>(val item: T)It makes your code reusable and clean, so you can handle many types easily with one smart class.
Think of a shopping cart app where you want to store different products. Using a generic class, you can create one cart item class that works for clothes, electronics, or groceries without rewriting code.
Manual classes for each type cause repeated code and mistakes.
Generic classes let one class work with many types.
This saves time and keeps code simple and flexible.