Extensions vs member functions priority in Kotlin - Performance Comparison
When Kotlin runs code with both extensions and member functions, it decides which one to use first.
We want to understand how this choice affects the time it takes to run the code.
Analyze the time complexity of the following code snippet.
class Example {
fun greet() = "Hello from member"
}
fun Example.greet() = "Hello from extension"
fun main() {
val e = Example()
println(e.greet())
}
This code shows a class with a member function and an extension function with the same name.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the function
greet()on an object. - How many times: Only once in this example, but the rule applies every time the function is called.
Each time the function greet() is called, Kotlin checks if a member function exists first.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to greet(), each directly calls member function |
| 100 | 100 calls to greet(), each directly calls member function |
| 1000 | 1000 calls to greet(), each directly calls member function |
Pattern observation: The time grows linearly with the number of calls, as each call directly invokes the member function.
Time Complexity: O(n)
This means the time to run grows directly with how many times you call the function.
[X] Wrong: "Extension functions run faster than member functions because they are outside the class."
[OK] Correct: Kotlin always calls member functions first if they exist, so calling a member function is direct and not slower than an extension.
Understanding how Kotlin chooses between member and extension functions shows your grasp of function calls and helps you write clear, predictable code.
What if the extension function had a different name than the member function? How would the time complexity change?
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
