visibility-modifiers-kotlin

Visibility/Access modifiers

Visibility/Access modifiers are the keywords used in kotlin, access modifiers controls the visibility of the and member in kotlin If an Class or function can see a member of class then it can access it and modify it.

We have different keywords for different access levels, which are follows:

  • Public
  • Internal
  • Protected
  • Private

Public is the default access modifier in kotlin, if you do not provide any access modifier

Unlike java, you can as many as public classes in Kotlin in same file.

Dynamic and Static Typing

Visibility/Access Modifiers inside Module


public : Public declaration in module are visible to all the places in module.

internal : visible inside the same module (a set of Kotlin files compiled together)

protected : protected access is not applicable for classes in kotlin

private : visible inside the file containing the declaration

// consider below all created inside file.kt fun function1() { } // public by default and visible everywhere in same and other module private fun functionOne() { } // visible inside file.kt internal fun functionTwo() { } // visible inside the same module var name = "Boo" // visible everywhere get() = field // visible inside file.kt private set(value) { // visible inside file.kt field = value } private class SomeClass { } // visible inside file.kt // you cannot make class private in java

Nullability in Kotlin

Visibility Modifiers inside Classes and Interfaces


public : visible to every modules and classes in kotlin

internal : visible to all the classes and functions inside a file

protected : visible inside the class and its subclasses

private : visible inside the declared class only, Extension function do not get to access the private and protracted member of the class open class BaseClass() { var a = "chercher tech" // public by default if you do not provide any access modifiers private var b = 2 // private to Base class protected open val c = 3 // visible to the Base and the Derived class internal val d = false // visible inside the same module protected fun sampleFunction() { } // visible to the Base and the Derived class } class DerivedClass: BaseClass() { // a, c, d, and sampleFunction() of the Base class are visible // b is not visible override val c = 18 // c is protected } fun main(args: Array<String>) { val base = BaseClass() // base.a and base.d are visible // base.b, base.c and base.sampleFunction() are not visible val derived = DerivedClass() // derived.c is not visible }

if condition in kotlin

Constructor Visibility in Kotlin

To specify a access modifier along with constructor, we have to explicitly write constructor keyword for the constructor.

You can have a private constructor in kotlin to cerate singleton and factory methods.

By default, all the constructor are public in kotlin class TestClass private constructor(val name: Int) { // private constructor } Public constructor class TestClass(val name: Int) { // public constructor }

Functions in kotlin

Little More about Access Modifiers

  • Local variable and function cannot have access modifiers
  • You can change the access modifier of a class member in derived class, but derived class override must provide more visible access
  • You cannot decrease access level when you override a function in derived class
open class BaseClass{ protected open var sampleValue = 10 internal open fun sampleFunction(){ println("base class value") } } class DerivedClass: BaseClass() { public override var sampleValue = 23 public open override fun sampleFunction() { println("derived class value") } }

Kotlin Standard Functions

aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech