0
0
Kotlinprogramming~5 mins

Visibility modifiers (public, private, internal, protected) in Kotlin

Choose your learning style9 modes available
Introduction

Visibility modifiers control who can see and use parts of your code. They help keep your code safe and organized.

When you want to hide details inside a class so others can't change them by mistake.
When you want some code to be used only inside the same file or module.
When you want to share code only with related classes but not everyone.
When you want everything to be open and accessible everywhere.
Syntax
Kotlin
public class MyClass {
    private val secret = "hidden"
    internal fun insideModule() {}
    protected open fun forSubclasses() {}
    public fun everyoneCanUse() {}
}

public means anyone can use it.

private means only inside the class.

internal means only inside the same module.

protected means only inside the class and subclasses.

Examples
This shows all four modifiers in one class.
Kotlin
class Example {
    private val secret = 42  // only inside Example
    internal fun moduleFun() {} // inside same module
    protected fun subclassFun() {} // inside Example and subclasses
    public fun openFun() {} // anywhere
}
Private class is only visible inside the file it is declared in.
Kotlin
private class HiddenClass {
    fun doSomething() {}
}

// HiddenClass is only visible inside this file.
This function can be used anywhere inside the same module but not outside.
Kotlin
internal fun moduleFunction() {
    println("Only inside this module")
}
Sample Program

This program shows how each visibility modifier controls access from inside the class, subclass, and outside.

Kotlin
open class Parent {
    private val privateVal = "private"
    internal val internalVal = "internal"
    protected val protectedVal = "protected"
    public val publicVal = "public"

    fun showValues() {
        println("Inside Parent:")
        println(privateVal)
        println(internalVal)
        println(protectedVal)
        println(publicVal)
    }
}

class Child : Parent() {
    fun showChildValues() {
        println("Inside Child:")
        // println(privateVal) // Error: privateVal not visible
        println(internalVal)    // visible
        println(protectedVal)   // visible
        println(publicVal)      // visible
    }
}

fun main() {
    val parent = Parent()
    parent.showValues()

    val child = Child()
    child.showChildValues()

    println("Outside classes:")
    // println(parent.privateVal) // Error: private
    println(parent.internalVal)  // visible inside module
    // println(parent.protectedVal) // Error: protected
    println(parent.publicVal)    // visible everywhere
}
OutputSuccess
Important Notes

By default, Kotlin declarations are public if no modifier is given.

private at top-level means visible only inside the file.

internal is useful to hide code from other modules but share inside your app.

Summary

public: visible everywhere.

private: visible only inside the class or file.

internal: visible inside the same module.

protected: visible inside class and subclasses.