liveBook · Manning

Private member

As explained in Kotlin in Action, 2E

The private modifier restricts access to a Kotlin declaration to its containing class or, for top-level declarations, its containing file.

A private declaration hides implementation details from dependent code. Kotlin permits access inside the containing class; for a top-level declaration, access is limited to the containing file. section 4.1.3

Class member versus external access

A `TalkativeButton` can declare `private fun yell()`, keeping that operation inside the class. The example also contrasts it with `protected fun whisper()`, which is intended for subclasses rather than unrelated external code.

How Kotlin in Action, 2E covers it

How deep it goes
Defines class and file visibility, then contrasts private with public, protected, internal, and Java package-private access.
Examples
Uses a TalkativeButton with private yell and protected whisper members, plus a visibility comparison table.
What you can do after
Apply private visibility to hide class implementation details or restrict top-level declarations to one file.
How it connects
Relates private visibility to encapsulation, public visibility, protected visibility, internal visibility, and Java package-private visibility.

Also explained in

Compare how 12 books explain private member →

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.