Kotlin Visibility Modifiers

Visibility modifiers decide who can see and use a piece of code. They protect important data, hide messy details, and keep a program easy to change. A well-chosen modifier works like a door lock: the right people enter, and everyone else stays out.

The House Analogy

Think of a house in a neighborhood. The front garden is open to everyone walking by. The living room welcomes family and guests inside the building. The family room belongs to relatives. A bedroom belongs to one person only. Kotlin maps these places to four modifiers.

 +-----------------------------------------------+
 | MODULE (the whole neighborhood project)       |
 |   +---------------------------------------+   |
 |   | FILE                                  |   |
 |   |   +-------------------------------+   |   |
 |   |   | CLASS                         |   |   |
 |   |   |  private    = bedroom         |   |   |
 |   |   |  protected  = family room     |   |   |
 |   |   +-------------------------------+   |   |
 |   +---------------------------------------+   |
 |   internal = inside the building              |
 +-----------------------------------------------+
 public = front garden, open to the world

The Four Modifiers

ModifierWho Can Use It
publicEveryone, everywhere. This is the default.
internalCode inside the same module
protectedThe class and its subclasses
privateThe class itself, or the file for top-level items

A module means one compiled unit, such as a Gradle project or an Android library.

Public by Default

Kotlin treats every declaration as public when you write no modifier. Writing public adds no new meaning.

class Greeter {
    fun hello() = "Hello"    // public
}

Private Protects Data

A private member lives inside its class. Outside code cannot read or change it. A bank account demonstrates why that matters.

class BankAccount {
    private var balance = 0.0

    fun deposit(amount: Double) {
        if (amount > 0) balance += amount
    }

    fun showBalance() = "Balance: $balance"
}

fun main() {
    val account = BankAccount()
    account.deposit(500.0)
    println(account.showBalance())   // Balance: 500.0
    // account.balance = 1000000.0   // Error: cannot access
}

The only road to the balance passes through deposit, and that function rejects negative amounts. The class guards its own rules.

Private Setters

A property can allow reading from everywhere and writing from inside only.

class Counter {
    var count = 0
        private set

    fun increase() { count++ }
}

fun main() {
    val c = Counter()
    c.increase()
    println(c.count)   // 1
    // c.count = 50    // Error
}

Protected Shares with Children

A protected member stays hidden from outsiders but opens for subclasses. Inheritance needs the open keyword on the parent class.

open class Animal {
    protected fun breathe() = "Breathing"
}

class Dog : Animal() {
    fun live() = breathe() + " and barking"
}

Protected does not exist for top-level functions or classes, because they have no parent class.

Internal Stays in the Module

Libraries use internal to hide helper code from the apps that depend on them. Inside the library, every file sees the helper. Apps outside never do.

internal class CacheManager {
    fun clear() { println("Cache cleared") }
}

Top-Level Private

A private top-level function or variable belongs to one file only.

private fun secretHelper() = 42

Private Constructors

A constructor can carry a modifier too. A private constructor blocks outside code from creating objects directly.

class Settings private constructor() {
    companion object {
        fun create() = Settings()
    }
}

Nested Classes

A nested class can use the private members of its outer class. The outer class cannot use the private members of its nested class.

Choosing the Right Level

  • Start with private and open access only when another class truly needs it.
  • Expose a small public surface, and keep the details hidden.
  • Use internal for library helpers.