Access Modifier in Kotlin

Learn via video courses
Topics Covered

Overview

While writing codes in Kotlin programming language, sometimes you come across a situation where you need to get access to a pre-written set of codes such as methods, properties, classes, etc. But there are some rules for accessing these components. Access modifiers are used to control the accessibility of these components in your entire program. In this article, you will see what is access modifiers in Kotlin, what are their different types and how you can use them. Before that, let us see a brief introduction of access modifiers in Kotlin.

Introduction

Access modifiers in Kotlin programming language are defined as a keyword that is used to control or restrict the use of various elements of the program such as class, methods, properties, interface, etc. You can use the access modifiers in Kotlin for various purposes such as a class header or method body.

Getters are defined as the function which is used to get the values of the properties. In Kotlin, you can not set the visibility of getters because it follows the same visibility of its properties.

There are basically four types of access modifiers in Kotlin which are as follows:

  1. public: As the name suggests, this modifier is used to make your class, methods, properties, etc. visible in your entire program. It means that you can access the public access modifiers anywhere in your code.
  2. private: This access modifier in Kotlin is used to access classes, methods, properties, etc. in that particular class only where it is defined.
  3. internal: This access modifier in Kotlin is used to access classes, methods, properties, etc. in the same module. Modules are the set of necessary Kotlin files that are compiled together.
  4. protected: The protected modifiers in Kotlin are used to access classes, methods, properties, etc. to the class where it is defined and also to its sub-classes.

These are the four types of modifiers that set the visibility of modifiers in Kotlin. Now let us see each of these access modifiers in Kotlin one by one briefly.

Public Modifier

public modifiers are the default access modifier in Kotlin. This is the most used modifier in the whole Kotlin programming language. Any other code in your program can access top-level items, such as classes, functions, or variables declared directly inside a package, when the public modifier is utilized for them. Any code that can access the container can access this element if the public modifier is applied to a nested element, for example, an inner class or a function inside a class.

  • Actually, you do not need to declare anything public in Kotlin because it is the default access modifier in Kotlin.
  • It means that if you declare any method, class, or object public or not it does not matter. Because it will be taken as public by default.

Let us see an example to understand the public access modifier in Kotlin.

Output:

Explanation: In the above example, there are two classes namely X and Y. You can access them anywhere in the entire code and the function show() can be accessed because class X and class Y are publicly accessible.

Private Modifier

private access modifier in Kotlin allows to access the code that is declared inside the same scope only. You can not access the methods, objects, etc. outside of the scope where it is declared. In Kotlin programming language, you can declare many top-level declarations in the same file and it allows you to access those elements in the code but it should be in the same file. Let us see an example for better understanding.

Output

Explanation: In the above example, you can access class A from within the same source file only. When you will try to access the int value outside the class, you will get an error in the output. The error will be a compile-time error.

Internal Modifier

Internal access modifier is a new add-on in the Kotlin language. It is not supported in Java programming language. When you use the internal keyword with an object, method, or property, it means you can access them inside that specific module only. Modules are a group of necessary files that are merged together to add more functionality to your program. You can access them inside the same module only.

If you try to access them outside that specific module, it will throw an error. The Internal access modifier in Kotlin also helps to write APIs and apply them in your code. Let us see an example to understand internal modifiers.

Explanation: In the above example, you can only access class X inside the module of the program. Here, Class X is only accessible from inside the same module. The variables int and function display() are only accessible from inside the same module, even though class Y can be accessed from anywhere.

Protected Modifier

A protected access modifier in Kotlin is used to restrict access to the object, methods, and properties for the subclasses of a class only. The protected modifier in Kotlin restricts access to the class itself and its subclasses, not necessarily to the whole package. The protected access modifier in Kotlin can not be used to support the top-level declarations. Let us see an example where you will see the use of the getvalue() function of the derived class.

Output:

Explanation: In the above example, there are two classes namely class X and class Y. Both these two classes are derived from each other. The code also declares an int variable in the protected scope of X that is initialized to 5. The main function creates an instance of the Y class and assigns it to x. In this way, you can create a new variable in the subclass that is not visible in the parent class by using the protected modifiers.

Overriding of Protected Modifier

As we know, a protected variable cannot be overridden in the derived class but you can use the keyword open to make this possible.

Given below is the example that shows how you can use the open keyword to access the protected variable and override them into the derived class.

Output:

Explanation: In the above example, there are two members in the class. One is an open variable, and the other is a protected variable. The open variable can be accessed from outside of the class, but the protected one cannot. The derived class overrides the value of int in its constructor to 10, then it returns this value in getvalue().

Visibility of Constructors

The visibility of a constructor is defined as the accessibility of those constructors in the whole program. Where we can access those or not? By default, it is public but you can modify the visibility of the constructor by the use of access modifiers in Kotlin. For example,

To do this, you need to use the appropriate keyword with the class name as shown below:

Advantages and Disadvantages of Using Visibility Modifiers

Visibility modifiers in Kotlin are used for various purposes according to the need of the programmer to manipulate and restrict the accessibility of objects, methods, properties, etc. Using them has both advantages and disadvantages. Let us see what are these advantages and disadvantages in the below section.

Advantages of Visibility Modifiers in Kotlin:

  • Modularity: Using visibility modifiers in Kotlin helps to make your code component modular. It allows the user to write your code once and you can use them as many times as you want in your code. Hence, increasing the code reusability.
  • Encapsulation: Using visibility modifiers in Kotlin helps to make your code component secure. You can hide and secure the functionality and features of the code component inside the access modifiers in Kotlin from the outer world. This helps to ensure the internal state of the class in your application code.
  • Abstraction: Using visibility modifiers in Kotlin helps to make your code component flexible. It helps to secure how the code is implemented behind the public interface. This allows you to make your code bug-free for a long time.

Disadvantages of Visibility Modifiers in Kotlin:

  • Complexity: Using visibility modifiers in Kotlin helps to make your code component complex. If there are many classes and there are different layers available in your code, then it becomes complex to deal with the different layers of code.
  • Overhead: Using visibility modifiers in Kotlin helps to make your code component face the overhead problem. While running the code, the compiler needs to do extra checking to imply the modifier visibility rules.

Conclusion

  • Access modifiers in Kotlin is defined as a keyword that is used to control or restrict the accessibility of class, methods, properties, interface, etc.
  • There are four types of access modifiers in Kotlin that are public, private, internal, and protected.
  • Internal access modifier is a new add-on in the Kotlin language. It is not supported in Java programming language. A protected access modifier in Kotlin is used to restrict access to the object, methods, and properties for the subclasses of a class only.