kotlin
  1. kotlin-visibility-modifier

Kotlin Visibility Modifier

Kotlin allows us to control the visibility of the classes, interfaces, methods, and properties using visibility modifiers. Visibility modifiers restrict access to the members of a class or interface from other parts of the program. In this tutorial, we'll discuss various visibility modifiers in Kotlin.

Syntax

There are four types of visibility modifiers available in Kotlin:

  • public: The public modifier is the default modifier in Kotlin. Any accessible class, function, or property in Kotlin that doesn't have any visibility modifier is public by default.
  • private: The private modifier is used to restrict access to a member within the class. A private member can be accessed only within the same file where it is declared.
  • protected: The protected modifier can be accessed within the same package as well as in subclasses.
  • internal: The internal modifier restricts the visibility of a member to the same module where it is declared. A module is a set of Kotlin files compiled together.

Example

Let's consider an example of a class with different visibility modifiers.

class Sample {
    private var x = 10
    protected var y = 20
    internal var z = 30
    var a = 40
}

fun main() {
    val obj = Sample()
    println(obj.a)
    // obj.x   --> gives a compile-time error because the variable is private 
    // obj.y   --> gives a compile-time error because the variable is protected
    // obj.z   --> gives a compile-time error because the variable is internal
}

Here, the visibility modifiers are used to restrict access to the members of the Sample class.

Output

When you run the above example, the output will be:

40

Explanation

In the example above, we have created a class called Sample. The class has four variables x, y, z, and a with different visibility modifiers.

  • The x variable is private, so it can be accessed only within the same class.
  • The y variable is protected, so it can be accessed within the same package and in subclasses.
  • The z variable is internal, so it can be accessed within the same module.
  • The a variable is public, so it can be accessed from anywhere in the program.

In the main() function, we have created an object of the Sample class and accessed the a variable. However, when trying to access x, y, and z variables, we get a compile-time error because they are not accessible outside of their respective modifiers.

Use

Visibility modifiers allow us to control the accessibility of the members of a class or interface. They help in maintaining the encapsulation and security of the codebase. They are useful while creating libraries or modules where we don't want to expose the implementation details outside of the package.

Important Points

  • The visibility modifiers can be used with classes, interfaces, member functions, and properties.
  • A public member can be accessed anywhere in the program.
  • A private member can be accessed only within the same class where it is declared.
  • A protected member can be accessed within the same package and in subclasses.
  • An internal member can be accessed within the same module where it is declared.

Summary

In this tutorial, we discussed the different visibility modifiers in Kotlin. We covered the syntax, example, output, explanation, use, and important points of visibility modifiers. With the knowledge of these visibility modifiers, we can control the accessibility of our codebase and maintain the encapsulation and security of our project.

Published on: