August 2017
Intermediate to advanced
440 pages
10h
English
When there is a member function and an extension function with the same name and parameters, the member function always wins. Here is an example:
class A {
fun foo() {
println("foo from A")
}
}
fun A.foo() {
println("foo from Extension")
}
A().foo() // Prints: foo from A
This is always true. Even methods from a superclass win with extension functions:
open class A {
fun foo() {
println("foo from A")
}
}
class B: A()
fun B.foo() {
println("foo from Extension")
}
A().foo() // foo from A
The point is that the extension function is not allowed to modify the behavior of a real object. We can only add extra functionalities. This keeps us secure, because we know that no one will change the behavior of objects that we are ...
Read now
Unlock full access