liveBook · Manning

Public visibility

As explained in Kotlin in Action, 2E

Visibility is the access-control property that determines which code can refer to a declaration.

Public visibility determines which code can refer to a declaration. In Kotlin, declarations are public unless another visibility modifier is specified, making public APIs concise while supporting explicit encapsulation where needed. section 4.1.3 section 2.2

A public Kotlin class

class Person(val name: String) The class, constructor, and property are public by default. Code anywhere can create Person instances and read name. section 2.2

Use visibility modifiers to control the boundary around implementation details:

  • public exposes a class member or top-level declaration everywhere. section 4.1.3
  • protected exposes a member to subclasses, while private limits a member to its class or a top-level declaration to its file. section 4.1.3
  • internal exposes declarations only within the compiled module, such as a Gradle source set, Maven project, or IntelliJ IDEA module. section 4.1.3
  • Kotlin has no Java-style package-private visibility; packages organize namespaces rather than control access. section 4.1.3
  • Library authors can enable explicit API mode with -Xexplicit-api={strict|warning} to require visibility and public API types to be declared explicitly. section 4.1.3

Public state with a private setter

class LengthCounter { var counter: Int = 0 private set fun addWord(word: String) { counter += word.length } } fun main() { val lengthCounter = LengthCounter() lengthCounter.addWord("Hi!") println(lengthCounter.counter) // 3 // lengthCounter.counter = 0 // Compile-time error } The public getter lets clients observe the counter, while the private setter prevents external code from storing an incorrect value. section 4.2.5

Table 4.2 Kotlin visibility modifiers (view table figure)

Modifier

Class member

Top-level declaration

public (default)

Visible everywhere

Visible everywhere

internal

Visible in a module

Visible in a module

protected

Visible in subclasses

—

private

Visible in a class

Visible in a file

Compare Kotlin visibility for class members and top-level declarations at a glance. section 4.1.3

How Kotlin in Action, 2E covers it

How deep it goes
Defines four modifiers, compares member and top-level rules, and demonstrates property accessors; it defers broader property features.
Examples
Uses a converted Person class, a visibility-modifier table, and a LengthCounter with a private setter.
What you can do after
Apply it in Kotlin to expose APIs deliberately and restrict mutation or implementation details with suitable modifiers.
How it connects
Connects public visibility with encapsulation, access modifiers, properties, modules, Java interoperability, and explicit API mode.

Also explained in

Compare how 8 books explain public visibility →

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.