Sobes.tech
Junior — Middle

Can you list and explain the main functions of scope in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, visibility scope determines where you can access classes, functions, properties, and other code elements. The main visibility modifiers are:

  • public — accessible everywhere, this is the default.
  • internal — accessible within a single module (for example, a project or library).
  • 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 = "accessible only within the class"
    protected open fun greet() = "Hello"
    internal val moduleData = 42
    public val alwaysVisible = "everywhere"
}

This control of visibility helps encapsulate implementation details and protect data from unwanted access.