Open In App

Kotlin Visibility Modifiers

Last Updated : 01 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

In Kotlin, visibility modifiers are used to control the visibility of a class, its members (properties, functions, and nested classes), and its constructors. The following are the visibility modifiers available in Kotlin:

  1. private: The private modifier restricts the visibility of a member to the containing class only. A private member cannot be accessed from outside the class.
  2. internal: The internal modifier restricts the visibility of a member to the same module. A module is a set of Kotlin files compiled together.
  3. protected: The protected modifier restricts the visibility of a member to the containing class and its subclasses.
  4. public: The public modifier makes a member visible to any code. This is the default visibility for members in Kotlin.

Note: If no visibility modifier is specified, Kotlin uses public by default.

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. 

Let's start discussing the above modifiers one by one.

1. Public Modifier

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.

Access Scope:

  • Accessible from any class or file
  • Works at both top-level and member-level declarations

Example:

Kotlin
// public by default
class A {
    val int1 = 10
    fun display() {
        println("Value: $int1")
    }
}

fun main() {
    val obj = A()
    obj.display()   // Accessible from anywhere
}

Here, Class A is accessible from anywhere in the entire code, the variable int1, and the function display() are accessible from anything that can access classes A.

2. Private Modifier

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.

Access Scope:

  • Accessible only within the same class or file

Example:

Kotlin
class A {
    private val int = 5
    fun show() {
        println("Inside A: $int")
    }
}

fun main() {
    val obj = A()
    println(obj.int)    // Error: Cannot access 'int': it is private in 'A'
    obj.show()          // Can be accessed
}

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. 

3. Internal Modifier

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.

Access Scope:

  • Accessible from any file within the same module
  • Not visible outside the module

Note: Internal modifier benefits in writing APIs and implementations. 

Example:

Kotlin
internal class A {
    internal val number = 100
    internal fun display() {
        println("Number is: $number")
    }
}

Here, Class A is only accessible from inside the same module. The variable number and function display() are only accessible from inside the same module.

4. Protected Modifier

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. 

Access Scope:

  • Accessible in the declaring class and its subclasses
  • Not visible outside the class hierarchy

Example:

Kotlin
open class A {
    protected val int = 10
}

class B : A() {
    fun getValue() {
        println("The value of integer is: $int") // Accessible in subclass
    }
}

fun main() {
    val obj = B()
    obj.getValue()
    println(obj.int)    // Error: Cannot access 'int': it is protected
}

Output: 

Cannot access 'int': it is protected

Overriding of Protected Modifier

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. 

Example:

Kotlin
open class A {
    protected open val int = 10
}

class B : A() {
    override val int = 20
    fun printValue() {
        println("The value of integer is: $int")
    }
}

fun main() {
    val obj = B()
    obj.printValue()
}

Output: 

The value of integer is: 20

Constructor Visibility

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
}

Advantages of using visibility modifiers in Kotlin

  1. Encapsulation: By restricting the visibility of the members of a class, you can enforce the principle of encapsulation and ensure that the internal state of the class remains hidden from the outside world.
  2. Modularity: By controlling the visibility of the members of a class, you can create modular components that can be easily reused and maintained.
  3. Abstraction: By hiding the implementation details of your classes behind public interfaces, you can create an abstraction layer that makes your code more maintainable and less prone to bugs.

Disadvantages of using visibility modifiers in Kotlin

  1. Complexity: Using visibility modifiers can make your code more complex, especially if you have many classes and members with different visibility levels.
  2. Overhead: Restricting the visibility of members can add some overhead to your code, as the compiler has to perform additional checks to enforce the visibility rules.

Next Article
Article Tags :

Similar Reads