What if your new function never runs because the original one silently takes priority?
Extensions vs member functions priority in Kotlin - When to Use Which
Imagine you have a class with some functions, and you want to add new behavior without changing the original class code. You try to add extra functions outside the class, but sometimes the program doesn't use your new functions as you expected.
Manually guessing which function will run--your added one or the original--can be confusing and cause bugs. You might waste time debugging why your new code is ignored, especially when both have the same name.
Kotlin's rule that member functions always have priority over extension functions clears this confusion. It helps you know exactly which function will run, making your code predictable and easier to maintain.
fun String.print() { println("Extension") } class Example { fun print() { println("Member") } } Example().print()
class Example { fun print() { println("Member") } } fun Example.print() { println("Extension") } Example().print()
This rule lets you safely add new functions to classes without accidentally overriding existing behavior, making your code clearer and more reliable.
When working with a library class you cannot change, you can add helpful functions as extensions. Knowing member functions have priority prevents unexpected behavior when the library updates its own functions.
Member functions always run before extension functions if both have the same name.
This rule avoids confusion and bugs when adding new functions to existing classes.
It helps keep your code predictable and easier to maintain.
Practice
Solution
Step 1: Understand member vs extension functions
Member functions are defined inside the class, while extension functions are defined outside to add functionality.Step 2: Check Kotlin's call priority
Kotlin always prefers member functions over extension functions when both have the same name.Final Answer:
The member function of the class -> Option AQuick Check:
Member functions have priority = A [OK]
- Thinking extension functions override member functions
- Assuming both functions run together
- Believing it causes a compile error
greet for class Person in Kotlin?Solution
Step 1: Recall extension function syntax
Extension functions are declared asfun ClassName.functionName().Step 2: Match syntax with options
fun Person.greet() { println("Hello") } matches the correct syntax:fun Person.greet().Final Answer:
fun Person.greet() { println("Hello") } -> Option DQuick Check:
Extension syntax = D [OK]
- Placing function name before class name
- Using 'extension' keyword incorrectly
- Wrong function declaration order
class Box {
fun size() = "Member size"
}
fun Box.size() = "Extension size"
fun main() {
val box = Box()
println(box.size())
}What is the output when this code runs?
Solution
Step 1: Identify member and extension functions
Class Box has a member functionsize()returning "Member size". An extension function with the same name returns "Extension size".Step 2: Determine which function is called
Kotlin calls the member function when both exist, sobox.size()calls the member function.Final Answer:
Member size -> Option BQuick Check:
Member function called over extension = C [OK]
- Expecting extension function output
- Thinking both outputs print
- Assuming compile error due to name clash
class Gadget {
fun info() = "Member info"
}
fun Gadget.info() = "Extension info"
fun main() {
val g: Gadget? = null
println(g?.info())
}What is the output when running this code?
Solution
Step 1: Analyze nullable variable and safe call
Variablegis null. The safe call operator?.prevents calling functions on null.Step 2: Understand safe call with extension/member functions
Sincegis null,g?.info()returns null without calling any function.Final Answer:
null -> Option AQuick Check:
Safe call on null returns null = B [OK]
- Expecting function output despite null
- Thinking NullPointerException occurs
- Confusing extension and member call on null
describe() function to class Device without modifying its source code. The class already has a member function describe(). How can you call the extension describe() function explicitly in Kotlin?Solution
Step 1: Understand member vs extension call priority
Kotlin always calls member functions over extensions if names clash; extension cannot override member.Step 2: Find workaround to call extension
To call extension explicitly, rename it or call via a wrapper function with a different name.Final Answer:
Call extension with a wrapper function or different name -> Option CQuick Check:
Rename or wrap extension to call it = A [OK]
- Trying to cast to call extension directly
- Expecting extension to override member
- Using reflection incorrectly to call extension
Ready to prove it?
Pass the test and this pattern moves unknown
