What if you could add new properties to any class instantly, without changing its original code?
Why Extension properties in Kotlin? - Purpose & Use Cases
Imagine you want to add a new feature to a class you didn't create, like adding a shortcut to get the full name from a Person object. Without extension properties, you'd have to create a new subclass or write helper functions everywhere.
Manually creating subclasses or helper functions is slow and clutters your code. It also makes your code harder to read and maintain because the new feature isn't directly tied to the original class.
Extension properties let you add new properties to existing classes without changing their code. This keeps your code clean and lets you use the new property just like a regular one.
fun getFullName(person: Person): String = person.firstName + " " + person.lastNameval Person.fullName: String get() = "$firstName $lastName"You can enhance existing classes with new, easy-to-use properties that feel like they belong there, making your code simpler and more expressive.
Adding a fullName property to a Person class lets you write person.fullName instead of calling a separate function, making your code cleaner and easier to understand.
Extension properties add new properties to existing classes without modifying them.
They keep your code clean and easy to read.
They help you write more natural and expressive code.
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
