0
0
KotlinConceptBeginner · 3 min read

Visibility Modifiers in Kotlin: What They Are and How to Use Them

In Kotlin, visibility modifiers control where classes, functions, and properties can be accessed from. The main modifiers are public, private, protected, and internal, each limiting access to different parts of your code.
⚙️

How It Works

Visibility modifiers in Kotlin work like doors that control who can enter a room. Imagine your code as a house with many rooms (classes, functions, properties). Each door (modifier) decides who can come inside.

Public means the door is open to everyone, so any part of your program can use that code. Private means the door is locked and only the owner (the class or file) can access it. Protected is like a door that only family members (subclasses) can open. Internal means the door is open only to people inside the same building (module).

This system helps keep your code safe and organized by hiding details that should not be changed or seen from outside.

💻

Example

This example shows how different visibility modifiers affect access to class members.

kotlin
open class Example {
    public val publicData = "I am public"
    private val privateData = "I am private"
    protected val protectedData = "I am protected"
    internal val internalData = "I am internal"
}

class SubExample : Example() {
    fun printData() {
        println(publicData)       // Accessible
        // println(privateData)   // Not accessible
        println(protectedData)    // Accessible because of inheritance
        println(internalData)     // Accessible within the same module
    }
}

fun main() {
    val example = Example()
    println(example.publicData)   // Accessible
    // println(example.privateData) // Error: private
    // println(example.protectedData) // Error: protected
    println(example.internalData)  // Accessible within the same module

    val subExample = SubExample()
    subExample.printData()
}
Output
I am public I am internal I am public I am protected I am internal
🎯

When to Use

Use visibility modifiers to protect your code and make it easier to maintain. Public is for things you want everyone to use, like library functions. Private hides details that should not be changed from outside, like helper functions or sensitive data.

Protected is useful when you want subclasses to access some parts but keep them hidden from others. Internal is great for sharing code within a module but not exposing it outside, helping to keep your project organized.

Think of it like organizing your workspace: keep tools you use often in open drawers (public), and keep fragile or private items locked away (private or protected).

Key Points

  • Public: accessible everywhere.
  • Private: accessible only inside the class or file.
  • Protected: accessible in the class and its subclasses.
  • Internal: accessible within the same module.
  • Use modifiers to control access and protect your code.

Key Takeaways

Visibility modifiers control access to classes and members in Kotlin.
Use public for open access and private to hide details inside a class.
Protected allows access in subclasses, internal limits access to the module.
Choosing the right modifier helps keep code safe and organized.