liveBook · Manning

Encapsulation

As explained in Kotlin in Action, 2E

Information hiding is the design principle of concealing implementation details behind a restricted public interface.

Encapsulation hides implementation details behind a restricted public interface, preserving control over which operations consumers can perform. section 16.3.1

Extending a class without breaking encapsulation

Kotlin extension functions can add behavior to existing JVM classes, including final classes, but they cannot access private or protected members. For example, `fun String.lastChar(): Char = get(length - 1)` adds a convenient operation while leaving String's hidden state protected; calling `"Kotlin".lastChar()` returns `n`. section 3.3

Expose read-only flow access

A class can keep `MutableSharedFlow<Int>` private and expose `messageFlow` as `SharedFlow<Int>`. Internal code emits through `_messageFlow`, while consumers can subscribe without gaining the ability to emit values. section 16.3.1

Listing 16.9 Broadcasting values using SharedFlow
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.random.*
import kotlin.time.Duration.Companion.milliseconds
 
class RadioStation {
    private val _messageFlow = MutableSharedFlow<Int>()  #1
    val messageFlow = _messageFlow.asSharedFlow()        #2
 
    fun beginBroadcasting(scope: CoroutineScope) {
        scope.launch {
            while(true) {
                delay(500.milliseconds)
                val number = Random.nextInt(0..10)
                log("Emitting $number!")
                _messageFlow.emit(number)                #3
            }
        }
    }
}

The complete RadioStation listing demonstrates the private mutable flow, public read-only view, and controlled emission boundary. section 16.3.1

How Kotlin in Action, 2E covers it

How deep it goes
Explains access boundaries through extension functions and private mutable versus public read-only flow types.
Examples
Uses String.lastChar and a RadioStation that privately emits shared-flow values for public subscribers.
What you can do after
Apply it via Kotlin visibility and flow types to prevent consumers from mutating internally controlled state.
How it connects
Connects extension functions, access modifiers, MutableSharedFlow, SharedFlow, and information hiding.

Also explained in

Compare how 14 books explain encapsulation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.