Sobes.tech
Junior — Middle

Can you name and describe the visibility scopes of various access modifiers in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, the following access modifiers define the visibility scope of elements:

  • public (default): accessible from anywhere in the project.
  • internal: accessible only within a single module (e.g., one Gradle module).
  • protected: accessible within the class and its subclasses.
  • private: accessible only within the file or class where it is declared.

Example:

class Example {
    private val secret = "private"
    protected val semiSecret = "protected"
    internal val moduleSecret = "internal"
    val publicInfo = "public" // public by default
}

Thus, modifiers allow controlling where and how variables, functions, and classes can be used, ensuring encapsulation and code security.

Can you name and describe the visibility scopes of… - sobes.tech