🐦 Kotlin Beginner

What is the open keyword in Kotlin?

Answer

In Kotlin, all classes and methods are final by default — they cannot be subclassed or overridden. The open keyword explicitly marks a class or method as inheritable/overridable. You write open class Animal to allow subclassing: class Dog : Animal(). Similarly, open fun sound() allows override fun sound() in a subclass. This design decision encourages composition over inheritance and prevents accidental subclassing of classes not designed for it, which is a common source of bugs in Java.