Visibility

Visibility Modifiers and Packages

The default visibility is public. private means this class (or this file, at top level); protected adds subclasses; internal means this module, a Gradle 19,597 source set whose tests can still see main's internals:

Four levels of visibility in BookNest's model package (Inventory.kt)Kotlin
package com.example.booknest.model
private const val MAX_STOCK = 99                   // visible in this file only
class Inventory {
  private val counts = mutableMapOf<Int, Int>()    // this class only
  var reserved: Int = 0                            // public getter...
    private set                                    // ...private setter
  internal fun restock(bookId: Int, n: Int) {      // same module only
    counts[bookId] = minOf((counts[bookId] ?: 0) + n, MAX_STOCK)
  }
  fun inStock(bookId: Int): Boolean = (counts[bookId] ?: 0) > reserved
}

A Main.kt that imports Inventory can call restock() and inStock(), but inv.reserved = 2 fails with cannot access 'reserved': it is private in 'com.example.booknest.model.Inventory'. Kotlin does not require directories to match packages, though its coding conventions recommend it.

can call the mangled name.