0
0
Kotlinprogramming~10 mins

Visibility modifiers (public, private, internal, protected) in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Visibility modifiers (public, private, internal, protected)
Start: Define class or member
Check modifier
public
Accessible everywhere
Use member accordingly
End
This flow shows how Kotlin decides who can access a class or member based on its visibility modifier.
Execution Sample
Kotlin
class Example {
    public val a = 1
    private val b = 2
    internal val c = 3
    protected val d = 4
}
Defines a class with four properties, each having a different visibility modifier.
Execution Table
StepMemberModifierAccessible FromExplanation
1apublicAnywherePublic means no restriction, accessible everywhere.
2bprivateOnly inside Example classPrivate restricts access to inside the class only.
3cinternalSame moduleInternal allows access within the same module.
4dprotectedExample class and subclassesProtected allows access in class and subclasses.
5Access outside ExampleCheck each modifierOnly public and internal accessible if in same modulePrivate and protected not accessible outside class or subclass.
6Access in subclassCheck protectedProtected accessibleProtected members accessible in subclass.
7End--All visibility rules applied.
💡 Execution ends after checking all visibility rules for each member.
Variable Tracker
MemberModifierAccessible From
apublicAnywhere
bprivateOnly inside Example class
cinternalSame module
dprotectedExample class and subclasses
Key Moments - 3 Insights
Why can't I access a private member from outside the class?
Private members are only accessible inside the class where they are declared, as shown in execution_table step 2.
What does internal mean and when can I access it?
Internal means the member is accessible anywhere inside the same module, but not outside it, as explained in step 3.
How is protected different from private?
Protected allows access in subclasses too, while private restricts access only to the class itself, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which member can be accessed anywhere without restriction?
Ab
Ba
Cc
Dd
💡 Hint
Check step 1 in execution_table where 'a' is public and accessible anywhere.
At which step does the table explain that a member is accessible only inside the class?
AStep 3
BStep 4
CStep 2
DStep 1
💡 Hint
Look at step 2 where 'b' is private and accessible only inside the class.
If you try to access a protected member from outside the class and subclass, what happens?
AIt is not accessible
BIt depends on the module
CIt is accessible
DIt becomes internal
💡 Hint
Step 5 and 6 explain protected is accessible only in class and subclasses, not outside.
Concept Snapshot
Visibility modifiers in Kotlin:
- public: accessible everywhere
- private: accessible only inside the class
- internal: accessible within the same module
- protected: accessible in class and subclasses
Use them to control who can see or use your code parts.
Full Transcript
In Kotlin, visibility modifiers control access to classes and members. Public means anyone can access it. Private means only the class itself can access it. Internal means access is allowed within the same module. Protected means access is allowed in the class and its subclasses. This helps keep code safe and organized by controlling who can use what. The example class shows four properties each with a different modifier. The execution table traces where each is accessible. Key moments clarify common confusions about private, internal, and protected. The quiz tests understanding by asking about accessibility at different steps.