English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin Visibility Modifiers

In this article, you will learn about4There are several visibility modifiers, as well as how they work in different situations.

Visibility modifiers are keywords used to set the visibility (accessibility) of classes, objects, interfaces, constructors, functions, properties, and their setters. (Visibility modifiers cannot be set for getters, as they always have the same visibility as the property)

InKotlin Classes and ObjectsIn this article, you have briefly learned about the visibility modifiers public and private. In this article, you will learn in detail about the other two visibility modifiers protected and internal (as well as public and private).

Visibility modifiers within a package

A package organizes a group of related functions, properties, and classes, objects, and interfaces.

ModifierDescription
publicVisible everywhere
privateVisible within the file containing the declaration
internalVisible within the same module (a set of Kotlin files compiled together)
protectedNot applicable to packages (used for subclasses)

Note:If no visibility modifier is specified, the default is public.

Let's take an example:

//File name: hello.kt
package test
fun function1() {}   //Public by default and visible everywhere
private fun function2() {}   //Visible within hello.kt
internal fun function3() {}   //Visible within the same module
var name = "Foo"   //Everywhere
    get() = field   //Visible within hello.kt (same as its properties)
    private set(value) {   //Visible within hello.kt
        field = value
    {}
private class class1 {}   //Visible within hello.kt

Visibility modifiers within classes and interfaces

The following is how visibility modifiers work for class-level declarations (functions, properties):

ModifierDescription
publicVisible to any client that can see the declared class
privateVisible only within the class
protectedVisible within the class and its subclasses
internalAny client within the module can see the declared class

Note:If you override a protected member in a derived class without specifying its visibility, its visibility will also be protected.

Let's take an example:

open class Base() {
    var a = 1                 //Public by default
    private var b = 2         // Base class is private
    protected open val c = 3  //Visible to Base class and Derived class
    internal val d = 4        //Visible within the same module
    protected fun e() { }     //Visible to Base class and Derived class
{}
class Derived: Base() {
    // a, c, d, and e() All properties of Base class are visible
    // b is not visible
    override val c = 9        // c is protected
{}
fun main(args: Array<String>) {
    val base = Base()
    //base.a and base.d are visible
    // base.b, base.c and base.e() are not visible
    val derived = Derived()
    // derived.c is not visible
{}

Change the Visibility of the Constructor

By default, the visibility of the constructor is public. However, you can change it. To do this, you need to explicitly add the constructor keyword.

In the following example, the constructor is set to public by default:

class Test(val a: Int) {
    // code
{}

You can change its visibility by the following methods.

class Test private constructor(val a: Int) {
    // code
{}

Here, the constructor is private.

Note:  In Kotlin, local functions, variables, and classes cannot have visibility modifiers.