Junior — Middle
Can you list the main types of access modifiers in Kotlin?
sobes.tech AI
Answer from AI
In Kotlin, the main access modifiers are:
public— accessible everywhere (default for all declarations).internal— accessible within a single module (a module is a set of files compiled together).protected— accessible in the class and its subclasses (only for class members).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 moduleWide = "internal"
val publicInfo = "public" // public by default
}
This set of modifiers allows flexible management of code visibility and encapsulation.