Left Index

Kotlin Access Specifiers

Visibility Modifiers are modifiers that when appended to a class/interface/property/function in Kotlin, would define where all it is visible and from where all it can be accessed. It is a language keyword that precedes a declaration and defines the scope from where it is visible -thus can be accessed.


There are four visibility modifiers in Kotlin

  • public modifiers
  • protected modifiers
  • private modifiers
  • internal modifiers

Public Modifiers

In Kotlin, a public modifier is the default modifier. Just like other language like C++, C# or Java public modifier, it means that the declaration is visible everywhere.

Example:
package com.theitbulls.kotlinex

public const val UNKNOWN_NAME = "Unknown"

fun main(args: Array<String>) {
	var student = Student()
	
	println("Name: ${student.name}")
	println("Roll No.: ${student.rollNo}")
	
	println("${UNKNOWN_NAME}")
}


public class Student {
	public var name: String = "Jony"
	public var rollNo: Int = 1
}
Output:
Name: Jony
Roll No.: 1
Unknown

Protected Modifiers

In Kotlin protected doesn't involve packages. It's purely the current class/interface and and sub classes. Protected properites and member function can access only same class or it's child class only.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var student = Student()

	student.displayInfo()
}

open class Parents {
	protected var fatherName: String = "Danial"
	protected var motherName: String = "Laila"
}

class Student : Parents() {
	public var name: String = "Jony"
	protected var rollNo: Int = 1

	fun displayInfo() {
		println("Name: ${name} S/0 ${fatherName} and Mother name: ${ motherName}")
		println("Roll No.: ${rollNo}")
	}
}
Output:
Name: Jony S/0 Danial and Mother name: Laila
Roll No.: 1

Private Modifiers

In Kotlin, private modifiers access only within scope. Out of scope can access. For example as shown in below image properties declared in Parents class i.e. fatherName can not access into it's child class i.e. Student.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var student = Student()

	student.displayInfo()
}

open class Parents {
	private var fatherName: String = "Danial"
	protected var motherName: String = "Laila"
	
	fun getFatherName() = fatherName
}

class Student : Parents() {
	public var name: String = "Jony"
	protected var rollNo: Int = 1

	fun displayInfo() {
		println("Name: ${name} S/0 ${getFatherName()} and Mother name: ${ motherName}")
		println("Roll No.: ${rollNo}")
	}
}
Output:
Name: Jony S/0 Danial and Mother name: Laila
Roll No.: 1

Internal Modifiers

In Kotlin, internal modifiers make class, properties to available for same module. We mean a group of files that are compiled together. Internal Modifiers is useful when you need to hide specific library implementations from the users. This was not possible using the package-private visibility in Java.


Note: Internal modifier benefits in writing API's and implementations.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var student = Student()

	student.displayInfo()
}

internal val sum = 0

internal class Student {
	public var name: String = "Jony"
	protected var rollNo: Int = 1
	
	internal var demo: Int = 10

	fun displayInfo() {
		println("Name: ${name}")
		println("Roll No.: ${rollNo}")
		
		println("${sum}")
	}
}