Visibility modifiers are keywords that set the visibility (accessibility) of classes, objects, interface, constructors, functions, properties and their setters. (You cannot set visibility modifier of getters as they always take the same visibility as that of the property.)
In Kotlin Class and Objects article, you learned about visibility modifiers public
and private
in brief. You will learn about two more visibility modifiers protected
and internal
(as well as public
and private
) in detail.
Visibility Modifiers Inside Package
A package organizes a set of related functions, properties and classes, objects, and interfaces. Recommended reading: Kotlin Packages
Modifier | Description |
---|---|
public | declarations are visible everywhere |
private | visible inside the file containing the declaration |
internal | visible inside the same module (a set of Kotlin files compiled together) |
protected | not available for packages (used for subclasses) |
Note: If visibility modifier is not specified, it is public
by default.
Let's take an example:
// file name: hello.kt package test fun function1() {} // public by default and visible everywhere private fun function2() {} // visible inside hello.kt internal fun function3() {} // visible inside the same module var name = "Foo" // visible everywhere get() = field // visible inside hello.kt (same as its property) private set(value) { // visible inside hello.kt field = value } private class class1 {} // visible inside hello.kt
Visibility Modifiers Inside Classes and Interfaces
Here's how visibility modifiers works for members (functions, properties) declared inside a class:
Modifier | Description |
---|---|
public | visible to any client who can see the declaring class |
private | visible inside the class only |
protected | visible inside the class and its subclasses |
internal | visible to any client inside the module that can see the declaring class |
Note: If you override a protected
member in the derived class without specifying its visibility, its visibility will also be protected
.
Let's take an example:
open class Base() { var a = 1 // public by default private var b = 2 // private to Base class protected open val c = 3 // visible to the Base and the Derived class internal val d = 4 // visible inside the same module protected fun e() { } // visible to the Base and the Derived class } class Derived: Base() { // a, c, d, and e() of the Base class are visible // b is not visible override val c = 9 // c is protected } fun main(args: Array<String>) { val base = Base() // base.a and base.d are visible // base.b, base.c and base.e() are not visible val derived = Derived() // derived.c is not visible }
Changing Visibility of a Constructor
By default, the visibility of a constructor is public
. However, you can change it. For that, you need to explicitly add constructor
keyword.
The constructor is public
by default in the example below:
class Test(val a: Int) { // code }
Here's how you can change its visibility.
class Test private constructor(val a: Int) { // code }
Here the constructor is private
.
Note: In Kotlin, local functions, variables and classes cannot have visibility modifiers.