Reading Time: 3 minutesA visibility modifier (a.k.a access modifiers or access specifiers) is a concept not tied to any specific programming language. It is a language keyword that precedes a declaration and defines the scope from where it is visible -thus can be accessed. Visibility Modifiers are modifiers that when appended to a class, interface, property or function in Kotlin, would define where all it is visible and from where all it can be accessed. The setters of properties in Kotlin can have a separate modifier from the property. There are four visibility modifiers in Kotlin
-
-
- private
- protected
- internal
- public
Private
The declarations marked with the private modifier are visible inside the file/class containing the declaration.
// Visible just inside this file
private const val numberThree = 3
// Visible just inside this file
private class User()
{
// Visible just inside the user class
private val numberEight = numberThree.plus(5)
}
// ERROR: numberEight is not accessible outside the User class
private const val numberEleven = numberEight.plus(numberThree)
Private
Protected
The declarations marked with the protected modifier are visible inside the file containing the declaration and subclasses. This modifier is not allowed for top-level declarations.
// Visible inside this file
private const val numberThree = 3
// Visible inside this file
private open class User() {
// Visible inside the User class and subclasses
protected val numberEight = numberThree.plus(5)
}
// Visible inside this file
private class Moderator() : User() {
// Visible inside the Moderator class
// numberEight is visible because Moderator is a subclass of User
private val numberEleven = numberThree.plus(numberEight)
}
// ERROR: protected modifier is not allowed for top level declarations
protected const val numberThree = 3
// ERROR: protected modifier is not allowed for top level declarations
protected class Staff()
Protected
Internal
The declarations marked with the internal modifier are visible everywhere in the same module.
// Internal.kt file
// Visible to everyone in the same module
internal const val numberThree = 3
// Visible to everyone in the same module
internal open class User() {
// Visible to everyone in the same module that has visibility on User
internal val numberEight = numberThree.plus(5)
}
// SameModule.kt file
// numberThree is visible because this file is in the same module than Internal.kt
// numberEight is visible because this file is in the same module than User
private const val numberEleven = numberThree.plus(User().numberEight)
// DifferentModule.kt file
// ERROR: numberThree is not visible because this file is in a different module than Internal.kt
// ERROR: numberEight is not visible because User() is in a different module than Internal.kt
private const val numberEleven = numberThree.plus(User().numberEight)
Internal
Public
The declarations marked with the public modifier are visible to everyone. public is the default visibility modifier in Kotlin (a declaration without explicit modifier will be public)
// Public.kt file
// Visible everywhere
public const val numberThree = 3
// Visible everywhere
public open class User() {
// Visible to everyone with visibility on the User class
public val numberEight = numberThree.plus(5)
}
// Visible everywhere
public class Moderator() {
// numberEleven is visible to everyone with visibility on the Moderator class
// numberEight is visible because User is public and numberEight is public
public val numberEleven = numberThree + User2().numberEight
}
/ AnotherFile.kt file
// numberThree is visible to any other file because it's public
// numberEight is visible to any other file because User and numberEight are public
// numberEleven is visible to any other file because Moderator and numberEleven are public
private const val numberTwentyTwo = numberThree + User().numberEight + Moderator().numberEleven
Public
Questions
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂
Click to rate this post!
[Total: 0 Average: 0]