0
0
Kotlinprogramming~10 mins

Extensions vs member functions priority in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Extensions vs member functions priority
Call function on object
Is member function available?
NoUse extension function
Yes
Call member function
End
When calling a function on an object, Kotlin first looks for a member function. If none is found, it uses an extension function.
Execution Sample
Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.greet() = "Extension"

fun main() {
    println(Example().greet())
}
This code shows that the member function 'greet' is called instead of the extension function.
Execution Table
StepActionFunction Found?Function CalledOutput
1Call greet() on Example instanceYes, member functionMember function greet()Member
2Print output--Member
💡 Member function exists, so extension function is ignored.
Variable Tracker
VariableStartAfter CallFinal
Example instanceCreatedUsed to call greet()No change
OutputNoneMemberMember
Key Moments - 2 Insights
Why does the member function get called instead of the extension function?
Because Kotlin always prefers member functions over extension functions when both exist, as shown in execution_table step 1.
What happens if the member function does not exist?
Then Kotlin calls the extension function if available, as the flow shows in the 'No' branch from member function check.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what function is called at step 1?
AExtension function greet()
BMember function greet()
CNo function is called
DBoth member and extension functions
💡 Hint
See execution_table row 1 under 'Function Called'
If the member function greet() was removed, which function would be called?
AMember function greet()
BNo function would be called
CExtension function greet()
DBoth functions would be called
💡 Hint
Refer to concept_flow where member function absence leads to extension function call
According to variable_tracker, what is the output after calling greet()?
A"Member"
BNo output
C"Extension"
DError
💡 Hint
Check variable_tracker row 'Output' after call
Concept Snapshot
Kotlin calls member functions before extension functions.
If a member function exists, extension with same name is ignored.
Extension functions are used only if no member function matches.
This rule helps avoid ambiguity and keeps member functions priority clear.
Full Transcript
In Kotlin, when you call a function on an object, the language first checks if the object has a member function with that name. If it does, Kotlin calls that member function. If not, Kotlin looks for an extension function with that name and calls it if found. This means member functions have higher priority than extension functions. For example, if a class has a member function greet(), and there is also an extension function greet() for that class, calling greet() on an instance will call the member function. If the member function is removed, then the extension function will be called instead. This priority rule helps keep function calls clear and predictable.