Careeroza
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).
| Modifier | Class member is visible... | Top-level declaration is visible... |
|---|---|---|
public (default) | everywhere | everywhere |
internal | inside the same module | inside the same module |
protected | in the class and its subclasses | not allowed |
private | inside the class only | inside 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.