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.


The instructions, techniques, and applications may be declared with a private modifier.

A non-public elegance or interface may be accessible only by its statistics individuals

personal class privateExample 
   personal val a = 1
   non-public val doSomething() 

In the above instance, the magnificence "privateExample" and the variable a each can be on hand best within the equal Kotlin record.


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

magnificence A() 
   protected val i = 1

class B : A() 
   a laugh getValue() : Int 
      return i

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


Internal bundle is visible best within the module beneath which it's far carried out. An internal class interface is visible most effective via other elegance gift inside the equal package or the module

Let's following example of enforce an internal method.

magnificence internalExample {
   inner val i = 1
   inner amusing doWhat() {
&#a hundred twenty five;

In the above instance, the approach named “doWhat” and the variable is noted as internal

these two fields may be accessible best within the package deal under which it is declared.


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
   amusing doWhat	&#forty;) {

   &#one hundred twenty five;

&#a hundred twenty five;

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.