👨🏼‍💻Kotlin Visibility Modifiers

What are the visibility modifiers in Kotlin?

Hüseyin Özkoç
Huawei Developers
3 min readJan 11, 2023

--

Kotlin Visibility Modifiers

Introduction

Hello, Dear Kotlin lovers! Welcome to my new article. I hope you are well and all is well. Today I’m going to talk about Visibility Modifiers in Kotlin. First, I would like to clarify the questions of what visibility modifiers are and what they are used for.

Visibility modifiers:

Visibility modifiers are keywords that determine the level of accessibility for various elements, such as classes, objects, interfaces, constructors, functions, properties, and their setters.

There are basically 4 types of visibility modifiers in Kotlin(There are 3 in Java. The internal visibility modifier is not available in Java). Furthermore, the default visibility in Kotlin is Public(Private by default in Java).

1-Public: Visibility modifier that shows it’s accessible from anywhere.

2-Private: It is a visibility modifier that cannot be accessed from any class other than the class it is defined in. (Even in Child classes)

3-Protected: Visibility modifier to indicate that it can only be accessed within its own class and by the classes from which it is inherited.

4-Internal: It is the visibility variable that makes the value public on a Module basis but private outside the module. (It is mostly used when writing a library because we may want to use our values everywhere in the library and not want that value to be accessed when switching to another module outside the library. Because if we are going to make a library and earn money on it, we do not want direct access to the functions we write from outside.)

If you are ready, let’s examine these visibility modifiers with examples.

Visibility modifiers with examples

As you can see, there are values defined with 4 different visibility modifiers in our Company class. In order to better see the status of these 4 values, we inherit our Company class from our Huawei class.

We were able to access all values except our private value in the Huawei class, which is our child class.

However, as we can see, we could not access values other than public and internal from the object of our Company class that we created on our main function. Likewise, we could not access our protected value from the Huawei class object that we created in the main function.

Additionally, our independent value and function definitions as Top level can take all visibility variables, but not the protected visibility variable.

Independent value and function definitions as Top level can take all visibility variables, but not the protected visibility variable.

Because logically, the value or function will never have a child class.

Conclusion

As a result, we have 4 visibility modifiers in Kotlin and the visibility modifier in Kotlin is public by default unless otherwise specified. Likewise, the visibility modifiers we have available can be used in different ways in many different scenarios. Therefore, we need to determine which visibility variable we will use and write it according to the situation at hand. I hope it was a useful article for you. See you in my other articles!

References

--

--