Kotlin vs Java @Overrides

Paul S. :

As shown here, https://stackoverflow.com/a/16639438/8949356, in Java you can override the function of a declared class when it is public

But I want to know how to do this exact same code in Kotlin, I've tried a lot but haven't found anywhere something on this topic. And I could just go and do this in Java but the rest of my code is in Kotlin, plus I just can't stay with this kind of doubt; Kotlin feels like a great tool to me and want to learn it all.

mel.j :

If you want to override method, Use override keyword.

Like this.

override fun addNumber(i : Int) {
  val sum = i+10
}

You must declare method that be overrided with open keyword. If you don't, You can't override it.

open fun addNumber(i : T)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86612&siteId=1