Visibility modifiers can be specified to classes, interfaces, constructors, functions, and properties. In Kotlin, the following visibility modifiers are available:
Visibility modifiers | Classes | Interfaces | Constructors | Functions | Properties |
private |
Visible only inside the file |
Visible only inside the file |
Visible only inside the file |
Visible only inside the file |
Visible only inside the file |
protected |
Visible inside file and in subclasses |
Visible inside file and in subclasses |
Visible inside file and in subclasses |
Visible inside file and in subclasses |
Visible inside file and in subclasses |
internal |
Visible everywhere inside the same module |
Visible everywhere inside the same module |
Visible everywhere inside the same module |
Visible everywhere inside the same module |
Visible everywhere inside the same module |
public | Visible to all (default) | Visible to all (default) | Visible to all (default) | Visible to all (default) |
Visible to all (default) |
The protected keyword cannot be used to control the visibility of the preceding modifier if it is declared directly under a package. This is because the protected keyword expects to subclass. A module in Kotlin can be a Maven project, a Gradle project, an IDE project, and so on; in simple terms, a set of Kotlin files compiled and grouped together.