0
0
KotlinComparisonBeginner · 4 min read

Public vs Private vs Protected vs Internal in Kotlin: Key Differences

In Kotlin, public means visible everywhere, private restricts visibility to the containing class or file, protected allows visibility in the class and its subclasses, and internal limits visibility to the same module. These modifiers control how accessible your code elements are across different parts of your program.
⚖️

Quick Comparison

This table summarizes the visibility scope of each Kotlin modifier.

ModifierVisibility ScopeAccessible FromDefault Modifier
publicAnywhereAny code in any moduleYes (default)
privateOnly inside the class or fileSame class or file onlyNo
protectedClass and subclassesSame class and subclassesNo
internalSame moduleAny code within the same moduleNo
⚖️

Key Differences

public is the most open modifier, allowing access from any other code, even outside the module. It is the default visibility if no modifier is specified.

private restricts access strictly to the class or file where the member is declared, hiding it from all other code. This is useful for encapsulating implementation details.

protected is like private but also allows subclasses to access the member, supporting inheritance. It is only applicable to class members, not top-level declarations.

internal restricts visibility to the same module, which is a set of Kotlin files compiled together. This is helpful for sharing code within a project but hiding it from external users.

⚖️

Code Comparison

Example showing public usage in Kotlin:

kotlin
open class Example {
    public val publicData = "Visible everywhere"
    private val privateData = "Visible only inside Example"
    protected val protectedData = "Visible in Example and subclasses"
    internal val internalData = "Visible inside the module"
}

fun main() {
    val example = Example()
    println(example.publicData) // Works
    // println(example.privateData) // Error: private
    // println(example.protectedData) // Error: protected
    println(example.internalData) // Works if in same module
}
Output
Visible everywhere Visible inside the module
↔️

Internal Equivalent

Example showing internal usage and its effect:

kotlin
internal class InternalExample {
    internal val data = "Accessible within module"
}

fun main() {
    val internalExample = InternalExample()
    println(internalExample.data) // Works inside the same module
}
Output
Accessible within module
🎯

When to Use Which

Choose public when you want your code to be accessible everywhere, like APIs or libraries. Use private to hide details inside a class or file, keeping your code safe from outside changes. Pick protected when you want subclasses to access members but keep them hidden from other classes. Use internal to share code within your project module but keep it hidden from external modules or libraries.

Key Takeaways

Use public for the widest accessibility across all modules.
Use private to hide implementation details inside a class or file.
protected allows subclass access but hides from other classes.
internal restricts visibility to the same module, useful for project encapsulation.
Choosing the right modifier helps maintain clean, safe, and understandable code.