As we discussed in Chapter 3, Understanding Programming Paradigms in Kotlin, Kotlin fully supports Object-Oriented Programming (OOP). This probably comes as no surprise, given the popularity of OOP in modern languages and Kotlin's ties to the JVM and other JVM-based languages such as Java.
OOP in Kotlin shares many similarities with Java, but there are important differences as well, as in the following examples:
- Numeric types in Kotlin are all classes, even if they are represented internally as primitive values.
- Classes are closed by default.
- Interfaces may contain properties.
Kotlin also aims to adopt learning and best practices from years of Java to make modeling data easier. Toward this goal, Kotlin provides additional modifiers for creating certain families of classes:
- Sealed classes provide compiler support for defining restricted class hierarchies
- Inline classes provide a minimal-overhead means of declaring simple type classes
- Object declarations provide built-in support for the singleton pattern
As with other aspects of the language, Kotlin also provides mechanisms for reducing the boilerplate required to define and create class instances. Features such as primary constructors, initialization blocks, and secondary constructors provide the flexibility to scale your implementations from very simple class declarations to much more complex cases.