Sobes.tech
Junior

Name the access modifiers in Kotlin.

sobes.tech AI

Answer from AI

In Kotlin, the following access modifiers exist, which define the visibility of classes, functions, and properties:

  • public — accessible from anywhere (default value).
  • internal — accessible only within the same 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 = "private"
    protected val semiSecret = "protected"
    internal val moduleSecret = "internal"
    val publicInfo = "public" // public by default
}

These modifiers help encapsulate data and control access to it.

Name the access modifiers in Kotlin. — Android - sobes.tech