What does private mean for a top-level declaration versus a class member in Kotlin?
answer
- Top-level private = file scope
- Member private = class scope
- Private is NOT visible to subclasses
- Same-class instances see each other's privates
- No Java analog for file-level private
basics
~10 sA private top-level declaration is visible only inside its own file. A private class member is visible only inside that class (and its members), not to subclasses or outside code.
solid answer
~40 s`private` has two scopes depending on where it's applied. At the **top level** (a file-scoped function, property, or class), `private` means visible only within the **same .kt file** — other files in the same package cannot see it. As a **class member**, `private` means visible only inside the **declaring class and its members** (including nested classes and the companion object). Crucially, a private member is **not** visible to subclasses — for that you'd use `protected`. Private members also aren't accessible from instances of the same class in *other* files since they belong to the class scope, but other instances of the *same* class within the class body can access each other's privates. This file-vs-class duality is a common interview gotcha because Java's `private` only ever means class-level.
go deeper
Knows private restricts to the class and is the most restrictive level.
Distinguishes top-level (file) private from member (class) private and knows subclasses can't see it.
Explains same-class cross-instance access and how extension functions interact with private scope.
Reasons about using file-private helpers to enforce encapsulation boundaries and reduce a module's effective API surface.
## Two meanings of `private` `private` is the most restrictive modifier, but its exact reach depends on **where** the declaration lives. ### 1. Top-level `private` → file scope A top-level declaration is one not nested inside a class — declared directly in a `.kt` file. ```kotlin // File: Util.kt private fun secret() {} // visible only in Util.kt private const val KEY = 1 // visible only in Util.kt private class Helper // visible only in Util.kt ``` Nothing outside `Util.kt` can reference `secret`, `KEY`, or `Helper` — **even files in the same package**. This is how Kotlin lets you keep file-local helpers truly local. ### 2. Member `private` → class scope A `private` member is visible only inside the **declaring class** and the members declared *within* it (nested classes, the `companion object`, etc.). ```kotlin class Account(private val balance: Int) { fun canAfford(cost: Int) = balance >= cost // OK: same class fun merge(other: Account): Int = balance + other.balance // OK: other's private is reachable here } ``` Key points: - **Subclasses cannot see** a `private` member. Use `protected` if a subclass needs it. - A class can access the `private` members of **another instance of the same class** (as in `merge` above) because access is checked against the *class*, not the *object*. - A `private` member is **not** visible from extension functions declared outside the class file. ## Contrast with Java In Java, `private` *only ever* means class-level — there is no file concept. Kotlin's top-level `private` (file scope) has no Java analog; the closest is a package-private top-level class, but that's still package-scoped, not file-scoped. ## Common gotchas - People expect a `private` top-level helper to be reachable from the same package — it is **not**. - People expect `private` to block sibling-instance access — it does **not**. ## Recall - Top-level `private` = file-local. - Member `private` = class-local, **not** subclass-visible. - Same-class instances can read each other's privates.
- Can a subclass access a private property of its superclass?No. private is restricted to the declaring class; the subclass needs protected (or internal/public) to access it.
- Can an extension function access a class's private members?Only if it's declared inside the same class or, for top-level privates, the same file scope; a top-level extension in another file cannot.
saying these in an interview costs you the question
- Saying private top-level is package-visible
- Claiming subclasses can see private members
- Saying one instance can't touch another instance's private of the same class
- Treating Kotlin private exactly like Java private with no file nuance
- Confusing private with protected