Kotlin – Visibility modifiers

Visibility modifiers are the keywords which are used to restrict the use of class, interface, methods, and property of Kotlin in the application. These modifiers are used at multiple places such as class header or method body.

In Kotlin, visibility modifiers are categorized into four different types:

  • public 
  • protected
  • internal
  • private

1 . Public modifier

public modifier is accessible from everywhere in the project. It is a default modifier in Kotlin. If any class, interface etc. are not specified with any access modifier then that class, interface etc. are used in public scope.

public class Example{
}
class Demo{
}
public fun hello()
fun demo()
public val x = 5
val y = 10

All public declaration can be placed at top of the file. If a member of class is not specified then it is by default public.

2. Protected modifier

protected modifier with class or interface allows visibility to its class or subclass only. A protected declaration (when overridden) in its subclass is also protected modifier unless it is explicitly changed.

open class Base{
    protected val i = 0
}

class Derived : Base(){
    fun getValue() : Int{
        return i
    }
}

In Kotlin, protected modifier cannot be declared at top level.

-> Overriding of protected types

open class Base {
    open protected val i = 5
}

class Another : Base() {
    fun getValue(): Int {
        return i
    }

    override val i = 10
}

3. Internal modifier

The internal modifiers are newly added in Kotlin, it is not available in Java. Declaring anything makes that field marked as internal field. The internal modifier makes the field visible only inside the module in which it is implemented.

internal class Example{
    internal val x = 5
    internal fun getValue(){
    }
}
internal val y = 10

In above, all the fields are declared as internal which are accessible only inside the module in which they are implemented.

4. Private modifier

private modifier allows the declaration to be accessible only within the block in which properties, fields, etc. are declare. The private modifier declaration does not allow to access the outside the scope. A private package can be accessible within that specific file.

private class Example {
    private val x = 1
     private valdoSomething() {
    }
}

In above class Example,val x and function doSomthing() are declared as private. The class “Example” is accessible from the same source file, “val x” and “fun doSomthing()” are accessible within Example class.

Example of Visibility Modifier :

open class Base() {
    var a = 1 // public by default
    private var b = 2 // private to Base class
    protected open val c = 3 // visible to the Base and the Derived class
    internal val d = 4 // visible inside the same module
    protected fun e() {} // visible to the Base and the Derived class
}

class Derived : Base() {
    // a, c, d, and e() of the 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
} 

I hope you found what you were looking for from this tutorial. If you want more Kotlin tutorials like this, then do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂

Leave a Comment

Your email address will not be published.