0
0
Kotlinprogramming~10 mins

Extension properties in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Extension properties
Define Extension Property
Use Extension Property on Object
Access Getter
Return Computed Value
Use Value in Expression
Extension properties add new properties to existing classes without changing their code, accessed like normal properties but computed via getters.
Execution Sample
Kotlin
val String.firstChar: Char
    get() = this[0]

fun main() {
    val name = "Kotlin"
    println(name.firstChar)
}
This code adds a property to String to get its first character and prints it.
Execution Table
StepActionEvaluationResult
1Define extension property 'firstChar' for StringN/AProperty ready to use
2Create variable 'name' with value "Kotlin"N/Aname = "Kotlin"
3Access 'name.firstChar'Call getter: this[0]'K'
4Print 'name.firstChar'Output valueK
💡 Program ends after printing the first character of the string.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
nameundefined"Kotlin""Kotlin""Kotlin"
name.firstCharundefinedundefined'K''K'
Key Moments - 2 Insights
Why can't extension properties have backing fields?
Extension properties only have getters (and optionally setters) but no storage because they are not part of the original class; see step 1 and 3 where the getter computes the value.
How does 'name.firstChar' access the first character?
It calls the getter defined in the extension property which returns this[0], shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'name.firstChar' at step 3?
Aundefined
B'o'
C'K'
D'n'
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution_table.
At which step is the extension property 'firstChar' defined?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when the property is defined.
If we change 'name' to an empty string, what happens when accessing 'name.firstChar'?
AThrows an exception
BReturns a space character
CReturns null
DReturns empty string
💡 Hint
Recall that accessing this[0] on an empty string causes an error; see step 3 evaluation.
Concept Snapshot
Extension properties add new properties to existing classes without modifying them.
They use getters (and optionally setters) but cannot have backing fields.
Access them like normal properties, but values are computed.
Syntax: val ClassName.propertyName: Type get() = ...
Useful for adding read-only or computed properties.
Full Transcript
Extension properties in Kotlin let you add new properties to existing classes without changing their code. You define them with a getter that computes the value. For example, adding a 'firstChar' property to String returns the first character. When you use the property, Kotlin calls the getter to get the value. Extension properties cannot store data themselves because they have no backing fields. This means they only compute and return values on demand. In the example, the property is defined first, then a string variable is created. Accessing the property calls the getter, which returns the first character. Finally, the program prints that character and ends.