0
0
Kotlinprogramming~5 mins

Extensions resolved statically in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that Kotlin extensions are resolved statically?
It means that extension functions are determined by the type of the variable at compile time, not by the actual object instance at runtime.
Click to reveal answer
intermediate
Given <code>open class A</code> and <code>class B : A()</code>, if you define an extension function for <code>A</code> and another for <code>B</code>, which one is called when you use a variable of type <code>A</code> holding an instance of <code>B</code>?
The extension function for A is called because extensions are resolved based on the variable's declared type, not the actual instance type.
Click to reveal answer
intermediate
Why can't extension functions be overridden in Kotlin?
Because extensions are resolved statically at compile time, they are not part of the class's virtual method table and thus cannot be overridden like member functions.
Click to reveal answer
beginner
How can you call a member function instead of an extension function if both have the same signature?
The member function always takes precedence over an extension function with the same signature when called on an instance.
Click to reveal answer
beginner
Explain with a simple code example how Kotlin resolves extensions statically.
open class A
class B : A()

fun A.foo() = "A"
fun B.foo() = "B"

fun main() {
    val a: A = B()
    println(a.foo()) // Prints "A" because extension is resolved by variable type
}
Click to reveal answer
In Kotlin, when you call an extension function on a variable, which type determines which extension is used?
AThe runtime type of the object
BThe declared type of the variable
CThe superclass type
DThe most derived type available
If a class has a member function and an extension function with the same name and parameters, which one is called?
AThe member function
BThe extension function
CBoth are called
DIt causes a compile error
Can extension functions be overridden in subclasses in Kotlin?
AYes, like normal member functions
BOnly if the subclass defines the same extension
CNo, because they are resolved statically
DOnly if marked open
What will this code print? open class A class B : A() fun A.foo() = "A" fun B.foo() = "B" fun main() { val a: A = B() println(a.foo()) }
A"A"
B"B"
CCompilation error
DRuntime exception
Why are Kotlin extension functions useful despite being resolved statically?
AThey change the runtime type of objects
BThey override existing member functions
CThey replace inheritance
DThey allow adding functions to classes without modifying them
Explain how Kotlin resolves extension functions and why this means they cannot be overridden.
Think about when the compiler decides which extension to call.
You got /4 concepts.
    Describe a scenario with classes A and B where B inherits A, and both have extension functions. What happens when you call the extension on a variable typed as A but holding B?
    Focus on the difference between declared type and actual instance.
    You got /4 concepts.