Kotlin Visibility Modifiers

28 Apr, 2022
In Kotlin, visibility modifiers are used to restrict the accessibility of classes, objects, interfaces, constructors, functions, properties, and their setters to a certain level. No need to set the visibility of getters because they have the same visibility as the property.There are four visibility modifiers in Kotlin. 
If there is no specified modifier then by default it is public. Let’s start discussing the above modifiers one by one.
In Kotlin, the default modifier is public. It is possibly the most frequently used modifier in the entire language and there are additional restrictions on who can see the element being modified. Unlike Java, in Kotlin there is no need to declare anything as public – it is the default modifier, if we don’t declare another modifier – public works the same in Kotlin, as in Java. When we apply the public modifier to top-level elements – classes, functions or variables declared directly inside a package, then any other code can access it. If we apply the public modifier to a nested element – an inner class, or function inside a class – then any code that can access the container can also access this element.
Kotlin
// by default publicclass A {                var int = 10} // specified with public modifierpublic class B {        var int2 = 20    fun display() {     println("Accessible everywhere")            }}
Here, Class A and B are accessible from anywhere in the entire code, the variables int, int2, and the function display() are accessible from anything that can access classes A and B.
In Kotlin, private modifiers allow only the code declared inside the same scope, access. It does not allow access to the modifier variable or function outside the scope. Unlike Java, Kotlin allows multiple top-level declarations in the same file – a private top-level element can be accessed by everything else in the same file.
Kotlin
// class A is accessible from same source fileprivate class A {    private val int = 10    fun display()    {        // we can access int in the same class        println(int)          println("Accessing int successful")    }}fun main(args: Array<String>){    var a = A()    a.display()    // can not access 'int': it is private in class A    println(a.int) }
Output: 
Cannot access 'int': it is private in 'A'
Here, Class A is only accessible from within the same source file, and the int variable is only accessible from the inside of class A. When we tried to access int from outside the class, it gives a compile-time error. 
In Kotlin, the internal modifier is a newly added modifier that is not supported by Java. Marked as internal means that it will be available in the same module, if we try to access the declaration from another module it will give an error. A module means a group of files that are compiled together.
Note: Internal modifier benefits in writing APIs and implementations. 
Kotlin
internal class A {}public class B {    internal val int = 10    internal fun display() {    }}
Here, Class A is only accessible from inside the same module. The variable int and function display() are only accessible from inside the same module, even though class B can be accessed from anywhere. 
In Kotlin, the protected modifier strictly allows accessibility to the declaring class and its subclasses. The protected modifier can not be declared at the top level. In the below program, we have accessed the int variable in the getvalue() function of the derived class. 
Kotlin
// base classopen class A {      // protected variable    protected val int = 10} // derived classclass B: A() {    fun getvalue(): Int {          // accessed from the subclass        return int            }} fun main(args: Array<String>) {    var a = B()    println("The value of integer is: "+a.getvalue())}
Output: 
The value of integer is: 10
We need to mark the protected variable or function using an open keywords to override in the derived class. In the below program, we have overridden the int variable. 
Kotlin
// base classopen class A {     // protected variable    open protected val int = 10 } // derived classclass B: A() {   override val int = 20    fun getvalue():Int {          // accessed from the subclass        return int            }} fun main(args: Array<String>) {    var a = B()    println("The overridden value of integer is: "+a.getvalue())}
Output: 
The value of integer is: 20
By default constructors are public, but we can also change the visibility of a constructor by using the modifiers.  
class A (name : String) {
      // other code
}
We must explicitly specify this by using the constructor keyword whilst changing the visibility.  
class A private constructor (name : String) {
      // other code
}
marktahu7
wish75584
Kotlin OOPs
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.