Classes and Their Generated Types

Explore Kotlin’s type system, including class-generated types, subtyping relationships, and the roles of Any? and Any.

The relation between classes and types

We say that classes generate types. Think of the User class. It generates two types. Can we name them both?

  • One is User, but the second is not Any (Any is already in the type hierarchy).

  • The second new type generated by the class User is User?. Yes, the nullable variant is a separate type.

There are classes that generate many more types: generic classes. The Box<T> class theoretically generates an infinite number of types.

Get hands-on with 1200+ tech skills courses.