Sobes.tech
Junior — Middle

Kuidas deklarerida Kotlinis klass, et seda saaks laiendada pärandklassides?

sobes.tech AI

Vastus AI-lt

In Kotlin, by default, all classes are final, meaning they cannot be inherited. To allow inheritance, a class must be declared with the open modifier.

Example:

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

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

Here, Animal is declared as open, which allows the Dog class to inherit from it and override methods.