What happens when a Kotlin class has both a member function and an extension function with the same name?
The member function always has higher priority and will be called instead of the extension function.
Click to reveal answer
beginner
Can extension functions override member functions in Kotlin?
No, extension functions cannot override member functions. Member functions take precedence even if an extension function has the same signature.
Click to reveal answer
intermediate
Why do member functions have priority over extension functions in Kotlin?
Because member functions are part of the class definition and have direct access to the class internals, Kotlin always prefers them over extensions which are resolved statically.
Click to reveal answer
intermediate
How are extension functions resolved in Kotlin?
Extension functions are resolved statically based on the declared type of the variable, not the runtime type.
Click to reveal answer
beginner
What is a practical use case for extension functions despite member functions having priority?
Extension functions allow adding new functionality to classes without modifying their source code, especially useful for classes from libraries or the Kotlin standard library.
Click to reveal answer
In Kotlin, if a class has a member function and an extension function with the same name, which one is called?
ABoth are called
BThe member function
CThe extension function
DIt causes a compile error
✗ Incorrect
Member functions have higher priority and are called instead of extension functions with the same name.
Can extension functions override existing member functions in Kotlin?
AOnly for open classes
BOnly if marked with override
CYes, always
DNo, extension functions cannot override member functions
✗ Incorrect
Extension functions cannot override member functions; member functions always take precedence.
How are extension functions resolved in Kotlin?
AStatically based on the variable's declared type
BBy the JVM bytecode
CBased on the object's runtime type
DDynamically at runtime
✗ Incorrect
Extension functions are resolved statically using the declared type of the variable.
Why might you use an extension function in Kotlin?
ATo add functionality to a class without modifying it
BTo override a member function
CTo change the class's internal state
DTo create a new class
✗ Incorrect
Extension functions let you add new functions to classes without changing their source code.
If a variable is declared as a superclass type but holds a subclass instance, which extension function is called?
AExtension function of the subclass
BMember function of the subclass
CExtension function of the superclass
DNo extension function is called
✗ Incorrect
Extension functions are resolved based on the declared type, so the superclass extension function is called.
Explain the priority between member functions and extension functions in Kotlin and why it matters.
Think about how Kotlin decides which function to call when names clash.
You got /3 concepts.
Describe how extension functions are resolved in Kotlin and how this affects their behavior compared to member functions.
Consider the difference between compile-time and runtime decisions.
You got /3 concepts.
Practice
(1/5)
1. In Kotlin, when a class has a member function and an extension function with the same name, which one is called by default?
easy
A. The member function of the class
B. The extension function
C. Both are called
D. It causes a compile-time error
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 A
Quick Check:
Member functions have priority = A [OK]
Hint: Member functions override extensions with same name [OK]
Common Mistakes:
Thinking extension functions override member functions
Assuming both functions run together
Believing it causes a compile error
2. Which of the following is the correct syntax to declare an extension function greet for class Person in Kotlin?
easy
A. fun greet() Person { println("Hello") }
B. fun greet.Person() { println("Hello") }
C. extension fun Person.greet() { println("Hello") }
D. fun Person.greet() { println("Hello") }
Solution
Step 1: Recall extension function syntax
Extension functions are declared as fun 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 D
Quick Check:
Extension syntax = D [OK]
Hint: Extension syntax: fun ClassName.functionName() [OK]
Common Mistakes:
Placing function name before class name
Using 'extension' keyword incorrectly
Wrong function declaration order
3. Consider the following Kotlin code:
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?
medium
A. Compilation error
B. Member size
C. Extension size
D. Runtime exception
Solution
Step 1: Identify member and extension functions
Class Box has a member function size() 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, so box.size() calls the member function.
Final Answer:
Member size -> Option B
Quick Check:
Member function called over extension = C [OK]
Hint: Member function called if name clashes [OK]
Common Mistakes:
Expecting extension function output
Thinking both outputs print
Assuming compile error due to name clash
4. Given this Kotlin code:
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?
medium
A. null
B. Member info
C. Extension info
D. NullPointerException
Solution
Step 1: Analyze nullable variable and safe call
Variable g is null. The safe call operator ?. prevents calling functions on null.
Step 2: Understand safe call with extension/member functions
Since g is null, g?.info() returns null without calling any function.
Final Answer:
null -> Option A
Quick Check:
Safe call on null returns null = B [OK]
Hint: Safe call on null returns null, no function called [OK]
Common Mistakes:
Expecting function output despite null
Thinking NullPointerException occurs
Confusing extension and member call on null
5. You want to add a 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?
hard
A. You cannot call the extension if member exists with same name
B. Use (device as Device).describe() to call extension
C. Call extension with a wrapper function or different name
D. Use Device::describe.invoke(device) to call extension
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 C
Quick Check:
Rename or wrap extension to call it = A [OK]
Hint: Rename extension or wrap it to call when member exists [OK]
Common Mistakes:
Trying to cast to call extension directly
Expecting extension to override member
Using reflection incorrectly to call extension
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.