Introduction
Visibility modifiers in Kotlin are used to control the visibility and accessibility of classes, objects, interfaces, constructors, functions, properties, and their setters. Kotlin provides four visibility modifiers: public
, private
, protected
, and internal
. This chapter will cover the syntax and usage of these visibility modifiers with examples.
Visibility Modifiers
1. public
The public
modifier is the default visibility in Kotlin. A public member is accessible from anywhere in the code.
Syntax
public class ClassName {
public var property: Type = value
public fun method() { ... }
}
Example
fun main() {
val person = Person("Rahul")
println(person.name)
person.displayInfo()
}
public class Person(public val name: String) {
public fun displayInfo() {
println("Name: $name")
}
}
Output:
Rahul
Name: Rahul
2. private
The private
modifier restricts the visibility to the declaring class or file. A private member is accessible only within the class or file where it is declared.
Syntax
class ClassName {
private var property: Type = value
private fun method() { ... }
}
Example
fun main() {
val person = Person("Priya")
person.displayInfo()
}
class Person(private val name: String) {
fun displayInfo() {
println("Name: $name")
}
}
Output:
Name: Priya
3. protected
The protected
modifier allows visibility to the declaring class and its subclasses. A protected member is accessible within its class and subclasses.
Syntax
open class ClassName {
protected var property: Type = value
protected fun method() { ... }
}
class SubClass : ClassName() {
fun subMethod() {
println(property)
method()
}
}
Example
fun main() {
val student = Student("Amit", 20)
student.displayInfo()
}
open class Person(protected val name: String) {
protected fun displayInfo() {
println("Name: $name")
}
}
class Student(name: String, val age: Int) : Person(name) {
fun showDetails() {
displayInfo()
println("Age: $age")
}
}
Output:
Name: Amit
Age: 20
4. internal
The internal
modifier restricts the visibility to the same module. An internal member is accessible within the same module.
Syntax
internal class ClassName {
internal var property: Type = value
internal fun method() { ... }
}
Example
fun main() {
val person = Person("Anjali")
person.displayInfo()
}
internal class Person(internal val name: String) {
internal fun displayInfo() {
println("Name: $name")
}
}
Output:
Name: Anjali
Example Program with Visibility Modifiers
Here is an example program that demonstrates various aspects of visibility modifiers in Kotlin:
fun main() {
// Public modifier example
val publicPerson = PublicPerson("Rahul")
println(publicPerson.name)
publicPerson.displayInfo()
// Private modifier example
val privatePerson = PrivatePerson("Priya")
privatePerson.displayInfo()
// Protected modifier example
val student = Student("Amit", 20)
student.showDetails()
// Internal modifier example
val internalPerson = InternalPerson("Anjali")
internalPerson.displayInfo()
}
// Public class
public class PublicPerson(public val name: String) {
public fun displayInfo() {
println("Name: $name")
}
}
// Private class
class PrivatePerson(private val name: String) {
fun displayInfo() {
println("Name: $name")
}
}
// Protected class
open class Person(protected val name: String) {
protected fun displayInfo() {
println("Name: $name")
}
}
class Student(name: String, val age: Int) : Person(name) {
fun showDetails() {
displayInfo()
println("Age: $age")
}
}
// Internal class
internal class InternalPerson(internal val name: String) {
internal fun displayInfo() {
println("Name: $name")
}
}
Output:
Rahul
Name: Rahul
Name: Priya
Name: Amit
Age: 20
Name: Anjali
Conclusion
In this chapter, you learned about the four visibility modifiers in Kotlin: public
, private
, protected
, and internal
. These modifiers control the accessibility of classes, objects, interfaces, constructors, functions, properties, and their setters. Understanding these visibility modifiers is essential for encapsulating your code and managing its accessibility in your Kotlin programs.