Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which visibility modifier in Kotlin allows a class member to be accessed from anywhere in the project?
easy
A. internal
B. private
C. public
D. protected

Solution

  1. Step 1: Understand the meaning of public

    The public modifier means the member is visible everywhere in the project.
  2. Step 2: Compare with other modifiers

    private restricts to class/file, internal restricts to module, protected restricts to class and subclasses only.
  3. Final Answer:

    public -> Option C
  4. Quick Check:

    Visible everywhere = public [OK]
Hint: Public means visible everywhere in the project [OK]
Common Mistakes:
  • Confusing private with public visibility
  • Thinking internal is visible everywhere
  • Assuming protected is public
2. Which of the following is the correct syntax to declare a private property named count in a Kotlin class?
easy
A. private val count: Int = 0
B. val private count: Int = 0
C. val count private: Int = 0
D. private count val: Int = 0

Solution

  1. Step 1: Recall Kotlin property declaration syntax

    Visibility modifier comes before val or var, so private val count: Int = 0 is correct.
  2. Step 2: Check other options for syntax errors

    Options B, C, and D have incorrect order of keywords, which is invalid in Kotlin.
  3. Final Answer:

    private val count: Int = 0 -> Option A
  4. Quick Check:

    Modifier before val/var = correct syntax [OK]
Hint: Put visibility modifier before val or var [OK]
Common Mistakes:
  • Placing private after val
  • Mixing order of keywords
  • Omitting colon before type
3. Consider the following Kotlin code:
open class Parent {
    protected val secret = "hidden"
}

class Child : Parent() {
    fun reveal() = secret
}

fun main() {
    val child = Child()
    println(child.reveal())
    // println(child.secret) // Line X
}

What will happen if the commented Line X is uncommented?
medium
A. Compilation error: Cannot access 'secret' outside the class or subclass
B. Prints 'hidden' without error
C. Runtime error: Illegal access
D. Compilation error: 'secret' is private

Solution

  1. Step 1: Understand protected visibility

    protected members are accessible inside the class and its subclasses, but not outside.
  2. Step 2: Analyze access in main function

    Accessing child.secret from outside the class hierarchy causes a compilation error.
  3. Final Answer:

    Compilation error: Cannot access 'secret' outside the class or subclass -> Option A
  4. Quick Check:

    Protected blocks external access [OK]
Hint: Protected is accessible only inside class and subclasses [OK]
Common Mistakes:
  • Assuming protected is public
  • Expecting runtime error instead of compile error
  • Confusing private and protected
4. Given this Kotlin code snippet:
class Example {
    private val secret = "top secret"
}

fun main() {
    val ex = Example()
    println(ex.secret)
}

What is the error and how to fix it?
medium
A. No error; code runs and prints 'top secret'
B. Error: Missing constructor; fix by adding constructor
C. Error: Syntax error; fix by adding semicolon
D. Error: Cannot access private property outside class; fix by changing to public

Solution

  1. Step 1: Identify visibility of secret

    The property secret is declared private, so it cannot be accessed outside the class.
  2. Step 2: Understand the error cause and fix

    Trying to print ex.secret from outside causes a compilation error. Changing private to public or providing a public method to access it fixes the error.
  3. Final Answer:

    Error: Cannot access private property outside class; fix by changing to public -> Option D
  4. Quick Check:

    Private blocks external access [OK]
Hint: Private hides members outside class; use public or accessor [OK]
Common Mistakes:
  • Thinking private allows outside access
  • Confusing syntax errors with visibility errors
  • Ignoring need for accessor methods
5. You have two Kotlin files in the same module: FileA.kt and FileB.kt. In FileA.kt, there is an internal function fun greet() = "Hello". Which of the following statements is true about calling greet() from FileB.kt?
hard
A. greet() cannot be called because internal means private
B. greet() can be called from FileB.kt because internal allows access within the same module
C. greet() cannot be called because internal restricts access to the same file only
D. greet() can be called only if FileB.kt inherits from FileA.kt

Solution

  1. Step 1: Understand internal visibility

    internal means visible anywhere inside the same module, regardless of file.
  2. Step 2: Apply to given files

    Since FileA.kt and FileB.kt are in the same module, greet() is accessible from FileB.kt.
  3. Final Answer:

    greet() can be called from FileB.kt because internal allows access within the same module -> Option B
  4. Quick Check:

    Internal = visible in module [OK]
Hint: Internal means visible anywhere in the same module [OK]
Common Mistakes:
  • Confusing internal with private or file-private
  • Thinking internal restricts to one file
  • Assuming inheritance needed for internal access
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard