in , ,

Kotlin Visibility Modifiers

Kotlin Visibility Modifiers
Kotlin Visibility Modifiers

In this tutorial, you will learn pretty much each of the 4 visibility modifiers in Kotlin and how they work in various situations.

Visibility modifiers are the keywords which are used to restrict the use of class, interface, methods, and property of Kotlin in the application.

Visibility modifiers are keywords that set the visibility (accessibility) of classes, objects, interface, constructors, functions, properties and their setters. (You can’t set visibility modifier of getters as they generally take the same visibility as that of the property.)

In Kotlin Class and Objects tutorial, you learned about visibility modifiers public and private in a nutshell. You will learn about two more visibility modifiers protected and internal (just as public and private) in detail.

Visibility Modifiers Inside Package

A package organizes a set of related functions, properties and classes, objects, and interfaces. Suggested reading: Kotlin Packages

publicdeclarations are visible everywhere
privatevisible inside the file containing the declaration
internalvisible inside the same module (a set of Kotlin files compiled together)
protectednot available for packages (used for subclasses)

Note: If visibility modifier isn’t indicated, it is public by default.

Let’s take an example:

// file name: hello.kt

package test

fun function1() {}   // public by default and visible everywhere

private fun function2() {}   // visible inside hello.kt

internal fun function3() {}   // visible inside the same module

var name = "Foo"   // visible everywhere
    get() = field   // visible inside hello.kt (same as its property)
    private set(value) {   // visible inside hello.kt
        field = value

private class class1 {}   // visible inside hello.kt

Visibility Modifiers Inside Classes and Interfaces

Here’s the means by which visibility modifiers works for individuals (functions, properties) pronounced inside a class:

publicvisible to any client who can see the declaring class
privatevisible inside the class only
protectedvisible inside the class and its subclasses
internalvisible to any client inside the module that can see the declaring class

Note: If you override a protected part in the determined class without indicating its visibility, its visibility will likewise be protected.

Let’s take an example:

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

Changing Visibility of a Constructor

By default, the visibility of a constructor is public. In any case, you can change it. For that, you need to explicitly add constructor watchword.

The constructor is public by default in the example beneath:

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

Here’s how you can change its visibility.

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.

Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Kotlin Inheritance

Kotlin Inheritance

Types of Guided Media

Types of Guided Media