Difference between open and override methods in Kotlin?

后端 未结 3 1128
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条回答
  •  北荒
    北荒 (楼主)
    2021-02-18 17:32

    By default, the functions in Kotlin are defined as final. That means you cannot override them. If you remove the open from your function v() than you will get an error in your class Derived that the function v is final and cannot be overridden.

    When you mark a function with open, it is not longer final and you can override it in derived classes.

提交回复
热议问题