Sobes.tech
Junior — Middle

Can the open modifier be applied to data classes in Kotlin?

sobes.tech AI

Answer from AI

By default, all classes in Kotlin are final (cannot be inherited). The open modifier allows a class to be inheritable.

You can apply the open modifier to any class except those declared with sealed, data, enum, or abstract modifiers (since inheritance is already allowed for abstract classes, open is not needed).

Example:

open class Animal {
    open fun sound() = println("Some sound")
}

class Dog : Animal() {
    override fun sound() = println("Bark")
}

Here, open allows classes and methods to be overridden.