Kotlin Visibility Modifiers 🎯

beginner
22 min

Kotlin Visibility Modifiers 🎯

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.

Understanding 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:

  1. public: Can be accessed from any place
  2. protected: Can be accessed within the same class and subclasses
  3. internal: Can be accessed within the same module
  4. private: Can be accessed only within the same class

Let's dive into each modifier with examples! 💡

Public Visibility Modifier 💡

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.

kotlin
// 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.") } }

Protected Visibility Modifier 💡

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.

kotlin
// 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 } }

Internal Visibility Modifier 💡

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.

kotlin
// 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 } }

Private Visibility Modifier 💡

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.

kotlin
// 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 } }

Quiz 💡

Quick Quiz
Question 1 of 1

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! 💡