Visibility Modifiers

    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 on the “top-level”, i.e. directly inside a package:

    • 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;
    • is not available for top-level declarations.

    Note: to use a visible top-level declaration from another package, you should still it.

    For members declared inside a class:

    • private means visible inside this class only (including all its members);
    • protected — same as private + visible in subclasses too;
    • public — any client who sees the declaring class sees its public members.

    Note that in Kotlin, outer class does not see private members of its inner classes.

    If you override a member and do not specify the visibility explicitly, the overriding member will also have protected visibility.

    To specify a visibility of the primary constructor of a class, use the following syntax (note that you need to add an explicit constructor keyword):

    Here the constructor is private. By default, all constructors are public, which effectively amounts to them being visible everywhere where the class is visible (i.e. a constructor of an internal class is only visible within the same module).

    Local declarations

    Local variables, functions and classes can not have visibility modifiers.

    • an IntelliJ IDEA module;
    • a Maven project;
    • a Gradle source set (with the exception that the test source set can access the internal declarations of main);
    • a set of files compiled with one invocation of the <kotlinc> Ant task.