Signup/Sign In
LAST UPDATED: AUGUST 5, 2020

Kotlin Access Modifiers

In this tutorial, we will cover visibility modifiers or access modifiers in Kotlin. Access modifiers or visibility modifiers are used to define the scope of a class, function, field, interface etc. Visibility modifier keywords restricts the use of classes, functions, properties etc and states whether they are available in their subclasses, in other files, classes, other packages or not.

In Kotlin, visibility modifiers can be applied to classes, constructors, objects, interfaces, functions, properties, and their setters. Getters have the same visibility as the property.

There are four visibility modifiers in Kotlin:

  • Private

  • Protected

  • Internal

  • Public

The default visibility modifier is public. It means if we haven't specified any visibility modifier for a class, function, property etc then by default the compiler will consider it as public.

The visibility modifiers have different meanings according to the place where they are placed. We will look at them one by one.

Visibility modifiers at top-level fields

Top-level fields are those that are placed outside the classes or interfaces. They are present directly inside any Kotlin file.

For top-level fields, the different visibility modifiers mean:

  • public - It is a default modifier and can be omitted. It means that the declarations are visible everywhere. They can be used in different files, classes, packages, etc.

  • internal - Internal marked declarations are visible inside the same module. A module means the set of Kotlin files compiled together. During project development we can have multiple modules within the same project. Other modules can be a library or helper application supporting the main application.

  • private - The declaration marked with private are only available inside the same Kotlin file. They cannot be used outside that .kt file.

The protected modifier is not available for top-level fields.

Visibility modifiers inside classes and interfaces

The functions, properties, objects inside a class have access to modifiers. Do note that visibility of the whole class or interface depends on its visibility modifier which is at top-level.

The visibility modifiers inside classes signify:

  • public - Anyone who can see the class can also see all its public declaration. It means public declarations are visible everywhere. They are visible in other files, classes, packages and even modules.

  • internal - Same as top-level fields, they are visible inside same module.

  • protected - Declarations marked with protected are visible only inside the class and its subclass. They are not visible outside class. It means a function marked with protected keyword cannot be used outside the class or its subclass.

  • private - The declarations marked with private keyword are only visible inside that class. They are not visible outside the class not even in subclasses.

Let us see an example of visibility modifiers in classes:

package oop
open class Employee {
    // Function visible only inside class
    private fun privateFunction(){
    }

    // Function visible in same
    internal fun internalFunction(){
    }

    // Function visible everyWhere
    fun publicFunction(){
    }

    // Function visible only in Employee class and its subclasses
    protected fun protectedFunction(){
    }
}

class Developer: Employee() {
    // privateFunction() is not visible here
}

fun main() {
    // privateFunction() and protectedFunction() are not visible here
}

* For inheritance concepts see next tutorial.

Summary

In this tutorial, we discussed access modifiers, namely public, private, internal, and protected, in detail. In the next tutorial, we will learn the concept of inheritance in Kotlin.



About the author:
I'm a writer at studytonight.com.