Visibility Modifiers

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: private, protected, internal and public. The default visibility, used if there is no explicit modifier, is public.

On this page, you’ll learn how the modifiers apply to different types of declaring scopes.

Packages

Functions, properties and classes, objects and interfaces can be declared on the “top-level”, i.e. directly inside a package:

  1. // file name: example.kt
  2. package foo
  3. fun baz() { ... }
  4. class Bar { ... }
  • If you do not specify any visibility modifier, public is 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;
  • protected is not available for top-level declarations.

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

Examples:

  1. // file name: example.kt
  2. package foo
  3. private fun foo() { ... } // visible inside example.kt
  4. public var bar: Int = 5 // property is visible everywhere
  5. private set // setter is visible only in example.kt
  6. internal val baz = 6 // visible inside the same module

Classes and Interfaces

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;
  • internal — any client inside this module who sees the declaring class sees its internal members;
  • 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 protected member and do not specify the visibility explicitly, the overriding member will also have protected visibility.

Examples:

  1. open class Outer {
  2. private val a = 1
  3. protected open val b = 2
  4. internal val c = 3
  5. val d = 4 // public by default
  6. protected class Nested {
  7. public val e: Int = 5
  8. }
  9. }
  10. class Subclass : Outer() {
  11. // a is not visible
  12. // b, c and d are visible
  13. // Nested and e are visible
  14. override val b = 5 // 'b' is protected
  15. }
  16. class Unrelated(o: Outer) {
  17. // o.a, o.b are not visible
  18. // o.c and o.d are visible (same module)
  19. // Outer.Nested is not visible, and Nested::e is not visible either
  20. }

Constructors

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):

  1. class C private constructor(a: Int) { ... }

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.

Modules

The 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 test source set can access the internal declarations of main);
  • a set of files compiled with one invocation of the <kotlinc> Ant task.