Sobes.tech
Junior — Middle

What is the visibility scope of different access modifiers in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, the following access modifiers and their scopes exist:

  • public (default): accessible everywhere.
  • internal: accessible within the same module (e.g., a single Gradle module).
  • protected: accessible in the class and its subclasses.
  • private: accessible only within the file (for top-level declarations) or within the class (for class members).

Example:

class Example {
    private val secret = 42
    protected open fun greet() = "Hello"
    internal val moduleData = "data"
    public val publicData = "public"
}

Thus, modifiers allow controlling visibility and encapsulation of code.

What is the visibility scope of different access… - sobes.tech