Everything else is just a tool to model your domain in a way that’s easier to read and reason about, and to produce much shorter code. Interfaces define module boundaries and can be mocked to enable simpler testing. Through classes, objects, and data structures, you can model your domain entities. Enums make a list of limited options crystal clear. Extensions allow you to add functionality to existing types, without having access to their original source code, giving advantage of better logic structure. With generics, you can write universal code, promoting reuse. And overloaded operators give that extra edge to common operations that benefit from succinct, crisp syntax.

Object types (classes)

Custom types that focus on behavior. Object types allow you to model your domain, so you can reason about it more easily. You can give things familiar names, group properties and operations, define relationships, and form as many layers of abstraction as you like. These are subjective representations of the author’s imagination. Oftentimes quite useful, as we are used to think in models of the world.

Inheritance

Classes can be derived from other classes to modify their methods and properties, if the parent class allows it. Only open declarations can be overridden and modified properties must be marked with the override keyword. Only one parent class is allowed, but it’s possible to implement multiple interfaces.

protected visibility starts to make sense for inheriting classes.

open class Base(
  p: Int,
  protected open val visibleByChildrenOnly: Boolean = true
) {
  open val visibleByAll: Boolean
    get() = true

  open fun makeLayeredCake() = "====="
}

open class Derived(
  p: Int,
  override var visibleByAll: Boolean = true
): Base(p) {
  public override val visibleByChildrenOnly: Boolean
    get() = false

  override fun makeLayeredCake(): String {
    return " === \n" + super.makeLayeredCake()
  }
}

Overriding can be disabled by a child using the final keyword.

open class FinalCake(): Derived(0) {
  final override fun makeLayeredCake(): String {
    return "  =  \n" + super.makeLayeredCake()
  }
}

[--]