Visibility Modifiers Overview

The Four Modifiers

Visibility modifiers decide which code can access a class, function or property. Kotlin's default is public (Java's default is package-private).

ModifierClass member is visible...Top-level declaration is visible...
public (default)everywhereeverywhere
internalinside the same moduleinside the same module
protectedin the class and its subclassesnot allowed
privateinside the class onlyinside the same file only

Why Hide Things?

  • Safety: nobody can put an object into an invalid state (negative balance).
  • Freedom to change: private code can be rewritten without breaking other files.
  • Clean API: autocomplete shows only what callers should use.

Tip: Make everything as private as possible, then open it up only when needed.