Difference between open and override methods in Kotlin?

后端 未结 3 1147
Happy的楠姐
Happy的楠姐 2021-02-18 17:07
open class Base {

    open fun v() {}

    fun nv() {}
}

class Derived() : Base() {

    override fun v() {}
}

This is an example. Can someone please

3条回答
  •  Happy的楠姐
    2021-02-18 17:21

    The open annotation on a class is the opposite of Java's final: it allows others to inherit from this class as by default all classes in Kotlin are final. [Source]

    Only after declaring a class as open we can inherit that class.

    A method can only be overridden if it is open in the base class. Annotation override signals the overriding of base method by inheriting class.

提交回复
热议问题