Extension properties let you add new properties to existing classes without changing their code. This helps you add useful features easily.
Extension properties in Kotlin
val ClassName.propertyName: PropertyType get() = // return some value
Extension properties cannot have backing fields, so you must define a getter.
You can add only read-only properties (val) or computed properties with custom getters.
firstChar to String that returns the first character.val String.firstChar: Char get() = this[0]
sum to a list of integers that returns the total sum.val List<Int>.sum: Int get() = this.sum()
This program adds two extension properties: firstChar for String and sum for List<Int>. It prints the first character of a string and the sum of a list of numbers.
fun main() { val name = "Kotlin" println("First character: ${name.firstChar}") val numbers = listOf(1, 2, 3, 4) println("Sum of numbers: ${numbers.sum}") } val String.firstChar: Char get() = this[0] val List<Int>.sum: Int get() = this.sum()
Extension properties do not actually modify the class; they are just syntax sugar.
You cannot store values in extension properties because they have no backing field.
Extension properties add new properties to existing classes without changing them.
They must have a getter and cannot store data.
Useful for adding computed or shortcut properties to classes you don't own.
Practice
Solution
Step 1: Understand extension properties concept
Extension properties allow adding new properties to classes without changing their original code.Step 2: Identify correct description
"A property added to an existing class without modifying its source code" correctly describes this as adding properties externally without modifying the class.Final Answer:
A property added to an existing class without modifying its source code -> Option DQuick Check:
Extension property = added externally [OK]
- Thinking extension properties store data inside the class
- Confusing extension properties with member properties
- Believing extension properties can replace existing ones
isEven for Int in Kotlin?Solution
Step 1: Recall extension property syntax
Extension properties useval/var Type.propertyName get() = ...without a body for setter if read-only.Step 2: Check options
val Int.isEven get() = this % 2 == 0 correctly usesval Int.isEven get() = this % 2 == 0. var Int.isEven = this % 2 == 0 wrongly usesvarwithout setter. fun Int.isEven() = this % 2 == 0 is a function, not a property. val isEven: Int get() = this % 2 == 0 misses receiver type.Final Answer:
val Int.isEven get() = this % 2 == 0 -> Option BQuick Check:
Extension property syntax = val Type.prop get() [OK]
- Using var without setter for extension property
- Writing extension property as a function
- Omitting receiver type before property name
val String.firstChar: Char
get() = this[0]
fun main() {
val word = "hello"
println(word.firstChar)
}Solution
Step 1: Understand extension property behavior
The extension propertyfirstCharreturns the first character of the string usingthis[0].Step 2: Trace the code output
Forword = "hello",word.firstCharreturns 'h', which is printed.Final Answer:
h -> Option CQuick Check:
Extension property returns first char = h [OK]
- Expecting full string instead of first character
- Confusing property with function call
- Thinking extension properties cause errors
var String.lastChar: Char
get() = this[length - 1]
set(value) { this[length - 1] = value }Solution
Step 1: Review extension property setter rules
Extension properties can have setters, but they cannot modify the original object if it is immutable.Step 2: Check String mutability
Strings in Kotlin are immutable; you cannot assign tothis[length - 1]. This causes a compilation error.Final Answer:
You cannot assign to a character in a String -> Option AQuick Check:
Strings immutable = cannot set chars [OK]
- Assuming extension properties can store data
- Thinking extension setters always cause errors
- Confusing syntax errors with immutability issues
isAdult to the Person class that returns true if age is 18 or more, otherwise false. Given data class Person(val name: String, val age: Int), which is the correct extension property implementation?Solution
Step 1: Define read-only extension property
Sinceageis a val, we can only create a read-only extension property with a getter.Step 2: Check options for correct syntax and logic
val Person.isAdult get() = age >= 18 correctly definesval Person.isAdult get() = age >= 18. var Person.isAdult get() = age >= 18; set(value) { age = if(value) 18 else 0 } tries to setagewhich is immutable. fun Person.isAdult() = age >= 18 is a function, not a property. val isAdult: Person get() = age >= 18 has wrong syntax and receiver placement.Final Answer:
val Person.isAdult get() = age >= 18 -> Option AQuick Check:
Read-only extension property with getter = val Person.isAdult get() = age >= 18 [OK]
- Trying to create setter for immutable property
- Confusing extension property with extension function
- Incorrect syntax for receiver type in property
Ready to prove it?
Pass the test and this pattern moves unknown
