Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero : Visibility modifiers in Kotlin

In my previous article, I wrote about classes in the Zero to Kotlin Hero series.

In this article, we would cover visibility modifiers in Kotlin.

Visibility modifiers are keywords that set the accessibility of classes, objects, interfaces, constructors, functions, properties and their setters.

There are four visibility modifiers in Kotlin:

  • public
  • private
  • protected
  • internal

Using Visibility Modifiers in Packages

Classes, Objects, Interfaces, functions and properties can be declared directly inside a package.

Packages are actually used to organize a set of classes, objects, interfaces, functions and properties that are related.

  • If a declaration is marked public, it is visible everywhere.
  • If a declaration is marked private, it is visible inside the file containing the declaration.
  • The protected declaration is not available for packages.
  • If a declaration is marked internal, it is visible everywhere in the same module. A module is a software component that contains one or more functions that are usually compiled together. A Kotlin module could be; an IntelliJ IDEA module, a maven project, a gradle source set or a set of files compiled with one invocation of the <kotlinc> Ant task.

If a visibility modifier is not specified, public would be used by default. Externally visible packages should be imported when they have to be used.

// file name: Main.kt
package adora

private fun sing() { ... } // visible inside Main.kt

public var age: Int = 22 // property is visible everywhere
private set // setter is visible only in Main.kt

internal val female = true // visible inside the same module

Using Visibility Modifiers in Classes and Interfaces

This section illustrates visibility for members declared inside a class or an interface.

  • If a declaration is marked public, it is visible to anyone that can see the declaring class.
  • If a declaration is marked private, it is only visible inside the class.
  • If a declaration is marked protected, it is visible inside the class and subclasses that inherit from it. I covered inheritance in my previous article.
  • If a declaration is marked internal, it is visible to anyone in the module who sees the declaring class (Visibility to the declaring class means visibility to its internal members.)
//SNIPPET FROM THE OFFICIAL KOTLIN DOCUMENTATION

open class Superclass {
    private val a = 1
    protected open val b = 2
    internal val c = 3
    val d = 4 // public by default
    
   protected class Nested {
       public val e: Int = 5
   }
}

class Subclass : Superclass() {
     // a is not visible
     // b, c and d are visible
     // Nested and e are visible

     override val b = 5 // 'b' is protected
}

class Unrelated(o: Outer) {
     // o.a, o.b are not visible
     // o.c and o.d are visible (same module)
     // Outer.Nested is not visible, and Nested::e is not visible either 
}

Visibility in Constructors

By default, a constructor is public. However, if we want to change its visibility, we need to explicitly add the constructor keyword to the class header (I wrote about class headers in my previous article). Constructors that are not public are declared below:

class privateClass private constructor(s: String) {  }

class protectedClass protected constructor(s: String) { }

class internalClass internal constructor(s: String) {  }

N.B: Classes with private constructors cannot be instantiated using that constructor.

 

Conclusion

We have seen some of the basics of Visibility Modifiers in Kotlin. If you want to see more examples on Kotlin Visibility Modifiers, please go to my GitHub. Get ready to know about Kotlin Android Extensions in the next article!

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *