Search⌘ K
AI Features

Classes and Their Generated Types

Explore the connection between classes and types in Kotlin's type system. Learn how classes generate types including nullable and generic variants, understand subtype and supertype relationships, and grasp how these concepts affect programming with Kotlin.

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. ...