Kotlin - Visibility Control

In this chapter we are able to learn one-of-a-kind styles of modifiers .

Generally four access modifiers to be had in Kotlin.

Access modifier is used to restrict the usage of the variables, methods and class used in the application. this modifier is applicable at multiple places such as in the class header or method declaration.


The classes, methods, and packages can be declared with a private modifier.
private class privateExample {
   private val i = 1
   private val doSomething() {

In the above instance, the magnificence "privateExample" and the variable a each can be on hand best within the equal Kotlin record. the class "privateExample" and the variable i both can be accessible only in the same Kotlin file.


A included class or interface is seen to its subclass only.

class A() {
   protected val i = 1
class B : A() {
   fun getValue() : Int {
      return i

In the above example, the variable "i" is asserted as blanketed, therefore, it is only seen to its subclass.


Internal is a newly added modifier introduced in Kotlin. An Internal package is visible only inside the module under which it is implemented.
class internalExample {
   internal val i = 1
   internal fun doSomething() {

Protected is another access modifier for Kotlin.the variable "i" is declared as protected, hence, it is only visible to its subclass.


If no get right of entry to modifier is exact, then through default it is going to be inside the public scope.

Let's following instance of the way to declare a public variable or technique.

class publicExample {
   val i = 1
   fun doSomething() {

In the above example, we've got now not noted any modifier, for that reason these types of methods and variables are through default public.