Welcome to the Kotlin Visibility Modifiers lesson! In this tutorial, we'll learn about how to control the accessibility of our Kotlin classes, properties, and functions using various visibility modifiers.
Visibility modifiers in Kotlin help us define the access level of our class members. By default, every member in Kotlin is public, but we can use the following modifiers to restrict access:
public: Can be accessed from any placeprotected: Can be accessed within the same class and subclassesinternal: Can be accessed within the same moduleprivate: Can be accessed only within the same classLet's dive into each modifier with examples! 💡
The public modifier is used when we want to give full access to our class members, i.e., they can be accessed from any place.
// A public class
class PublicClass {
// A public property
public var publicProperty: String = "Public Property"
// A public function
public fun publicFunction() {
println("This is a public function.")
}
}The protected modifier restricts access to class members to the same class and its subclasses. This is useful when we want to share data or functionality with subclasses while keeping it hidden from the outside world.
// A base class with protected members
class BaseClass {
protected var protectedProperty: String = "Protected Property"
protected fun protectedFunction() {
println("This is a protected function.")
}
}
// A subclass with access to protected members
class SubClass : BaseClass() {
fun accessProtected() {
println(protectedProperty) // Accessing protected property
protectedFunction() // Calling protected function
}
}The internal modifier allows access to class members within the same module only. This is useful when we want to share data or functionality with other classes within the same module but keep it hidden from the outside world.
// An internal class
internal class InternalClass {
internal var internalProperty: String = "Internal Property"
internal fun internalFunction() {
println("This is an internal function.")
}
}
// Another class from a different module cannot access internal members
class AnotherClass {
// Compilation error: Internal members cannot be accessed from a different module
fun accessInternal() {
val internalInstance = InternalClass()
println(internalInstance.internalProperty) // Compilation error
internalInstance.internalFunction() // Compilation error
}
}The private modifier restricts access to class members to the same class only. This is useful when we want to keep data or functionality local to the class.
// A private class
private class PrivateClass {
private var privateProperty: String = "Private Property"
private fun privateFunction() {
println("This is a private function.")
}
}
// Another class cannot access private members
class AnotherClass {
// Compilation error: Private members cannot be accessed from outside the class
fun accessPrivate() {
val privateInstance = PrivateClass()
println(privateInstance.privateProperty) // Compilation error
privateInstance.privateFunction() // Compilation error
}
}Which modifier restricts access to class members to the same class only?
With this lesson, you now have a good understanding of the different visibility modifiers in Kotlin. Use these modifiers wisely to make your code more organized, maintainable, and secure! ✅
Happy coding! 💡