Education
Software Development Executive - I
Last updated on Sep 3, 2024
Last updated on Aug 12, 2024
Kotlin visibility modifiers are crucial for controlling the accessibility of various elements in your code. They define where your classes, objects, interfaces, constructors, functions, and properties can be accessed from, ensuring encapsulation and proper data hiding. Kotlin provides four primary visibility modifiers: private, protected, internal, and public.
Let's dive into each of these in detail.
The public modifier is the default visibility in Kotlin. This means that if you do not specify a visibility modifier, the element is public by default. Public members are accessible from anywhere in the codebase, both within the same module and from external modules.
1class PublicClass { 2 public val publicVal = "I am public" 3 fun publicFunction() { 4 println(publicVal) 5 } 6} 7 8fun main() { 9 val instance = PublicClass() 10 instance.publicFunction() // Accessible everywhere 11}
The private modifier restricts the visibility to the containing class or the same file for top-level declarations. This ensures that the private members are not accessible from outside the scope they are defined in.
1class PrivateClass { 2 private val privateVal = "I am private" 3 4 private fun privateFunction() { 5 println(privateVal) 6 } 7 8 fun accessPrivateFunction() { 9 privateFunction() // Accessible within the class 10 } 11} 12 13fun main() { 14 val instance = PrivateClass() 15 // instance.privateFunction() // Error: Cannot access 'privateFunction': it is private in 'PrivateClass' 16 instance.accessPrivateFunction() // Access through a public function 17}
The protected modifier allows visibility within the declaring class and its subclasses. It does not apply to top-level declarations. The protected modifier is particularly useful when you want to expose certain members to subclasses while hiding them from other classes.
1open class ProtectedClass { 2 protected open val protectedVal = "I am protected" 3 4 protected open fun protectedFunction() { 5 println(protectedVal) 6 } 7} 8 9class DerivedClass : ProtectedClass() { 10 override val protectedVal = "I am overridden protected" 11 12 override fun protectedFunction() { 13 println(protectedVal) 14 } 15} 16 17fun main() { 18 val instance = DerivedClass() 19 // instance.protectedFunction() // Error: Cannot access 'protectedFunction': it is protected in 'ProtectedClass' 20}
The internal modifier restricts the visibility to the same module. A module in Kotlin can be defined as a set of Kotlin files compiled together. This modifier is useful when you want to encapsulate code within a module but make it accessible to all files within the same module.
1internal class InternalClass { 2 internal val internalVal = "I am internal" 3 4 internal fun internalFunction() { 5 println(internalVal) 6 } 7} 8 9fun main() { 10 val instance = InternalClass() 11 instance.internalFunction() // Accessible within the same module 12}
When used inside classes, the visibility modifiers control access to class members:
private: Accessible only within the class.
protected: Accessible within the class and subclasses.
internal: Accessible within the same module.
public: Accessible from anywhere.
1open class VisibilityDemo { 2 private val privateVal = "Private" 3 protected open val protectedVal = "Protected" 4 internal val internalVal = "Internal" 5 val publicVal = "Public" // public by default 6 7 protected open fun protectedFunction() { 8 println(protectedVal) 9 } 10 11 internal fun internalFunction() { 12 println(internalVal) 13 } 14 15 fun publicFunction() { 16 println(publicVal) 17 } 18} 19 20class SubVisibilityDemo : VisibilityDemo() { 21 override val protectedVal = "Overridden Protected" 22 23 override fun protectedFunction() { 24 println(protectedVal) 25 } 26} 27 28fun main() { 29 val demo = VisibilityDemo() 30 val subDemo = SubVisibilityDemo() 31 32 // println(demo.privateVal) // Error: Cannot access 'privateVal': it is private in 'VisibilityDemo' 33 // println(demo.protectedVal) // Error: Cannot access 'protectedVal': it is protected in 'VisibilityDemo' 34 println(demo.internalVal) // Accessible within the same module 35 println(demo.publicVal) // Accessible everywhere 36 37 demo.publicFunction() 38 subDemo.protectedFunction() // Accessible in subclass 39}
By default, constructors in Kotlin are public. However, you can modify the visibility of constructors using the constructor keyword.
1class CustomConstructor private constructor(val a: Int) { 2 // This constructor is private 3} 4 5fun main() { 6 // val instance = CustomConstructor(1) // Error: Cannot access 'CustomConstructor': it is private in 'CustomConstructor' 7}
Understanding and properly using Kotlin visibility modifiers is essential for writing clean, maintainable, and secure code. By leveraging private, protected, internal, and public modifiers, you can control access to your code elements and encapsulate functionality appropriately. Whether you are working within the same module or across multiple modules, Kotlin's visibility modifiers provide the flexibility needed to manage access effectively.
For a deeper comparison of Kotlin access modifiers versus Java, check out our detailed blog on Kotlin Access Modifiers vs. Java.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.