0
0
Kotlinprogramming~10 mins

Extensions resolved statically in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extensions resolved statically
Call extension function on variable
Check variable's declared type
Select extension function based on declared type
Execute selected extension function
Return result
When calling an extension function, Kotlin uses the variable's declared type to decide which function to run, not the actual object type.
Execution Sample
Kotlin
open class Base
class Derived : Base()

fun Base.foo() = "Base"
fun Derived.foo() = "Derived"

val b: Base = Derived()
println(b.foo())
Shows that calling foo() on a Base-typed variable holding a Derived object calls Base's extension.
Execution Table
StepVariableDeclared TypeActual Object TypeExtension Function SelectedOutput
1bBaseDerivedBase.foo()"Base"
2println(b.foo())---Prints "Base"
3End---Execution stops after print
💡 Extension function chosen by declared type 'Base', not actual type 'Derived'
Variable Tracker
VariableStartAfter 1Final
bnullDerived instance (typed as Base)Derived instance (typed as Base)
Key Moments - 2 Insights
Why does b.foo() call Base.foo() even though b holds a Derived object?
Because extension functions are resolved using the variable's declared type (Base), not the actual object type (Derived), as shown in execution_table step 1.
Does Kotlin support polymorphism with extension functions?
No, extension functions are resolved statically at compile time based on declared type, so they do not behave polymorphically like member functions.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which extension function is called at step 1?
ABase.foo()
BDerived.foo()
CBoth Base.foo() and Derived.foo()
DNo extension function is called
💡 Hint
See execution_table row 1, column 'Extension Function Selected'
At which step does the program print the output?
AStep 1
BStep 2
CStep 3
DNo output is printed
💡 Hint
Check execution_table row 2, column 'Output'
If variable b was declared as Derived instead of Base, which extension function would be called?
ABase.foo()
BBoth Base.foo() and Derived.foo()
CDerived.foo()
DNo extension function would be called
💡 Hint
Extension function depends on declared type, see concept_flow and execution_table step 1
Concept Snapshot
Kotlin extension functions are resolved statically.
They use the variable's declared type, not the actual object type.
This means no polymorphism for extensions.
Example: Base.foo() called on Base-typed variable even if holding Derived.
Remember: extensions are compile-time resolved helpers.
Full Transcript
In Kotlin, when you call an extension function on a variable, the program looks at the variable's declared type to decide which extension function to run. It does not check the actual type of the object stored in the variable. For example, if you have a variable declared as Base but it holds a Derived object, calling an extension function defined for Base will run, not the one for Derived. This is because extension functions are resolved statically at compile time, unlike member functions which are polymorphic. The code example shows a Base.foo() extension and a Derived.foo() extension. When calling foo() on a Base-typed variable holding a Derived object, Base.foo() runs. This behavior is important to understand to avoid confusion about which function runs. Extensions are like helper functions tied to types, not to objects at runtime.