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.
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)
You can override functions and properties with the override
keyword. Note though, that in Kotlin, classes are not extensible by default, so you have to declare the parent with open
(i.e. open class MyClass()
) if you want to extend it (Java classes are still extensible unless final
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With