Classes, objects, interfaces, constructors, functions, properties and their setters can have visibility modifiers. Getters always have the same visibility as the property.
There are four visibility modifiers in Kotlin:
public. The default visibility is
On this page, you'll learn how the modifiers apply to different types of declaring scopes.
Functions, properties and classes, objects and interfaces can be declared at the "top-level" directly inside a package:
If you do not specify any visibility modifier,
publicis used by default, which means that your declarations will be visible everywhere.
If you mark a declaration
private, it will only be visible inside the file containing the declaration.
If you mark it
internal, it is visible everywhere in the same module.
protectedis not available for top-level declarations.
Classes and interfaces
For members declared inside a class:
privatemeans visible inside this class only (including all its members).
protectedis the same as
privatebut is also visible in subclasses.
internalmeans that any client inside this module who sees the declaring class sees its
publicmeans that any client who sees the declaring class sees its
If you override a
protected member and do not specify the visibility explicitly, the overriding member will also have
To specify a visibility of the primary constructor of a class, use the following syntax:
Here the constructor is private. By default, all constructors are
public, which effectively amounts to them being visible everywhere where the class is visible (this means that a constructor of an
internal class is only visible within the same module).
Local variables, functions and classes can't have visibility modifiers.
internal visibility modifier means that the member is visible within the same module. More specifically, a module is a set of Kotlin files compiled together:
an IntelliJ IDEA module
a Maven project
a Gradle source set (with the exception that the
testsource set can access the internal declarations of
a set of files compiled with one invocation of the