We use mutable and immutable interfaces to control if data can be changed or not. This helps keep our programs safe and clear.
Mutable vs immutable interfaces in Kotlin
interface ReadOnlyList<T> { fun get(index: Int): T val size: Int } interface MutableList<T> : ReadOnlyList<T> { fun add(item: T) fun removeAt(index: Int) }
Immutable interfaces only allow reading data, no changes.
Mutable interfaces extend immutable ones and add methods to change data.
interface ReadOnlyList<T> { fun get(index: Int): T val size: Int }
interface MutableList<T> : ReadOnlyList<T> { fun add(item: T) fun removeAt(index: Int) }
val readOnly: ReadOnlyList<String> = ... // readOnly.get(0) works // readOnly.add("new") does NOT work
val mutable: MutableList<String> = ... mutable.add("new") // works mutable.get(0) // works
This program shows a mutable list that you can add to and remove from. It prints the size and first item before and after removal.
interface ReadOnlyList<T> { fun get(index: Int): T val size: Int } interface MutableList<T> : ReadOnlyList<T> { fun add(item: T) fun removeAt(index: Int) } class SimpleList<T> : MutableList<T> { private val items = mutableListOf<T>() override fun get(index: Int) = items[index] override val size get() = items.size override fun add(item: T) { items.add(item) } override fun removeAt(index: Int) { items.removeAt(index) } } fun main() { val list: MutableList<String> = SimpleList() list.add("apple") list.add("banana") println("Size: ${list.size}") println("First item: ${list.get(0)}") list.removeAt(0) println("Size after removal: ${list.size}") println("First item now: ${list.get(0)}") }
Immutable interfaces help avoid bugs by preventing unwanted changes.
Mutable interfaces should be used carefully to keep data consistent.
In Kotlin, many standard collections have both mutable and read-only versions.
Immutable interfaces allow only reading data.
Mutable interfaces allow changing data and usually extend immutable ones.
Use immutable interfaces to protect data and mutable ones when changes are needed.
Practice
Solution
Step 1: Understand interface roles
Immutable interfaces provide read-only access to data, preventing changes.Step 2: Compare mutable interfaces
Mutable interfaces extend immutable ones and allow data to be changed.Final Answer:
Immutable interfaces allow only reading data, while mutable interfaces allow data modification. -> Option DQuick Check:
Immutable = read-only, Mutable = read-write [OK]
- Confusing which interface allows modification
- Thinking both interfaces allow changes
- Assuming immutable interfaces are only for primitives
Solution
Step 1: Identify mutable list declaration
MutableList is the interface for mutable lists, and mutableListOf() creates a mutable list instance.Step 2: Check each option
val list: MutableList<Int> = mutableListOf(1, 2, 3) correctly declares a MutableList and initializes it with mutableListOf().Final Answer:
val list: MutableList<Int> = mutableListOf(1, 2, 3) -> Option CQuick Check:
MutableList + mutableListOf() = mutable list [OK]
- Using listOf() with MutableList type
- Declaring mutable list as List type
- Mixing mutable and immutable functions
val immutableList: List<Int> = listOf(1, 2, 3) val mutableList: MutableList<Int> = mutableListOf(4, 5, 6) println(immutableList.size) mutableList.add(7) println(mutableList)
Solution
Step 1: Check immutable list size
immutableList has 3 elements, so size is 3.Step 2: Add element to mutable list and print
mutableList adds 7, so it becomes [4, 5, 6, 7].Final Answer:
3 [4, 5, 6, 7] -> Option AQuick Check:
Immutable size 3, mutable list adds 7 [OK]
- Trying to add to immutable list causing error
- Expecting immutable list size to change
- Confusing output format
val numbers: List<Int> = listOf(1, 2, 3) numbers.add(4)
Solution
Step 1: Understand List immutability
List interface in Kotlin is immutable; it does not support add() method.Step 2: Analyze the error
Calling add(4) on immutable List causes a compilation error.Final Answer:
Cannot add to a List because it is immutable -> Option AQuick Check:
Immutable List blocks add() method [OK]
- Trying to add to immutable List
- Assuming var allows modification of immutable List
- Confusing syntax error with immutability error
List<String> parameter but you want to modify the list inside the function. Which is the best approach to handle this in Kotlin?Solution
Step 1: Understand parameter immutability
List<String> is immutable, so it cannot be modified directly.Step 2: Evaluate safe modification methods
Casting to MutableList is unsafe and may cause runtime errors; changing parameter type affects API design.Step 3: Use safe copy for modification
Creating a mutable copy inside the function allows safe modifications without affecting original list.Final Answer:
Create a new mutable copy of the list inside the function and modify that. -> Option BQuick Check:
Copy immutable list to mutable for safe changes [OK]
- Casting immutable list to mutable unsafely
- Changing parameter type breaking API contracts
- Assuming val allows modification
Ready to prove it?
Pass the test and this pattern moves unknown
