Kotlin Visibility Modifiers

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.

public modifier –

In Kotlin, the default modifier is public. It is possibly the most frequently used modifier in the entire language and there is 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 unless we don’t declare another modifier. Except this, public works same in Kotlin, as in Java.

When we apply the public modifier to top-level elements- class or function or variable 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

class A {             // by default public
    var int = 10
}
public class B {     // specified with public modifier
    var int2 = 20
    fun display() {  
    println("Accessible everywhere")         
    }
}

chevron_right


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.

private modifier –

In Kotlin, private modifiers allows only the code declared inside the same scope can access it. It does not allow to access the modifier variable or function outside the scope.
Unlike Java, Kotlin allows multiple top-level declaration in the same file – a private top-level element can be accessed by everything else in the same file.

filter_none

edit
close

play_arrow

link
brightness_4
code

// class A is accessible from same source file
private class A {
    private val int = 10 
    fun display()
    {
        println(int)   // we can access int in the same class
        println("Accessing int successful")
    }
}
fun main(args: Array<String>){
    var a = A()
    a.display()
    println(a.int// can not access 'int': it is private in class A
}

chevron_right


Output:

10
Accessing int successful

Here, Class A is only accessible from within the same source file, and the int variable is only accessible from inside of class A. When, we tried to access int outside the class it gives compile time error.

protected modifier –

In Kotlin, the protected modifier strictly allow 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 derived class.

filter_none

edit
close

play_arrow

link
brightness_4
code

// base class
open class A {
    protected val int = 10 // protected variable
}
// derived class
class B: A() {
    fun getvalue(): Int {
        return int         // accessed from the subclass
    }
}
fun main(args: Array<String>) {
    var a = B()
    println("The value of integer is: "+a.getvalue())
}

chevron_right


Output:


The value of integer is: 10

Overriding of protected modifier –

We need to mark the protected variable or function using open keyword to override in the derived class. In the below program, we have overridden the int variable.

filter_none

edit
close

play_arrow

link
brightness_4
code

// base class
open class A {
    open protected val int = 10 // protected variable
  
}
// derived class
class B: A() {
   override val int = 20
    fun getvalue():Int {
        return int         // accessed from the subclass
    }
}
fun main(args: Array<String>) {
    var a = B()
    println("The overridden value of integer is: "+a.getvalue())
}

chevron_right


Output:

The value of integer is: 20

internal modifier –

In Kotlin, internal modifier is the newly added modifier which is not supported by Java. Marking as internal means that it will be available in the same module only if we try to access the declaration in another module it will give error. A module means a group of files that are compiled together.

Note: Internal modifier benefits in writing APIs and implementations.

filter_none

edit
close

play_arrow

link
brightness_4
code

internal class A {
}
public class B {
    internal val int = 10
    internal fun display() {
    }
}

chevron_right


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 the class B can be accessed from anywhere.

Constructor Visibility –

By default constructors are public, but we can also change the visibility of constructor using the modifiers.

class A (name : String) {
      // other code
}

We must explicitly specify with constructor keyword while changing the visibility.

class A private constructor (name : String) {
      // other code
}


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.