Kotlin - Visibility Control

In this chapter we will learn different types of modifiers .

Generally four access modifiers available in Kotlin.


The classes, methods, and packages can be declared with a private modifier.

A private class or interface can be accessible only by its data members

private class privateExample {
   private val a = 1
   private val doSomething() {
In the above example, the class "privateExample" and the variable a both can be accessible only in the same Kotlin file.


A protected class or interface is visible 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 declared as protected, hence, it is only visible to its subclass.


Internal package is visible only inside the module under which it is implemented. An internal class interface is visible only by other class present inside the same package or the module

Let's following example of implement an internal method.

class internalExample {
   internal val i = 1
   internal fun doWhat() {

In the above example, the method named “doWhat” and the variable is mentioned as internal

these two fields can be accessible only inside the package under which it is declared.


If no access modifier is specified, then by default it will be in the public scope.

Let's following example of how to declare a public variable or method.

class publicExample {

   val i = 1
   fun doWhat	() {



In the above example, we have not mentioned any modifier, thus all these methods and variables are by default public.