0
0
Kotlinprogramming~5 mins

Extensions vs member functions priority in Kotlin - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
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
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
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
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
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
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.