Kotlin tutorial for Beginners – Kotlin Visibility Modifiers

Hits: 1

Kotlin Visibility Modifiers

In this article, you will learn about all 4 visibility modifiers in Kotlin and how they work in different scenarios.

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

In Kotlin Class and Objects article, you learned about visibility modifiers public and private in brief. You will learn about two more visibility modifiers protected and internal (as well as public and private) in detail.


Visibility Modifiers Inside Package

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

Modifier Description
public declarations are visible everywhere
private visible inside the file containing the declaration
internal visible inside the same module (a set of Kotlin files compiled together)
protected not available for packages (used for subclasses)

Note: If visibility modifier is not specified, 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 how visibility modifiers works for members (functions, properties) declared inside a class:

Modifier Description
public visible to any client who can see the declaring class
private visible inside the class only
protected visible inside the class and its subclasses
internal visible to any client inside the module that can see the declaring class

Note: If you override a protected member in the 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         // 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. However, you can change it. For that, you need to explicitly add constructor keyword.

The constructor is public by default in the example below:

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.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.