0
0
Kotlinprogramming~5 mins

Visibility modifiers (public, private, internal, protected) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the public visibility modifier mean in Kotlin?
The public modifier means the declaration is visible everywhere. It is the default visibility if no modifier is specified.
Click to reveal answer
beginner
Explain the private visibility modifier in Kotlin.
The <strong>private</strong> modifier restricts the visibility to the containing class or file. It means the declaration cannot be accessed outside that scope.
Click to reveal answer
intermediate
What does internal visibility mean in Kotlin?
internal means the declaration is visible everywhere in the same module but not outside it. A module is a set of Kotlin files compiled together.
Click to reveal answer
intermediate
Describe the protected visibility modifier in Kotlin.
<strong>protected</strong> means the declaration is visible in the class and its subclasses. It is not visible outside these classes, even if in the same package.
Click to reveal answer
beginner
Which visibility modifier is the default in Kotlin if none is specified?
The default visibility modifier in Kotlin is public, meaning the declaration is visible everywhere.
Click to reveal answer
Which Kotlin visibility modifier allows access only within the same file?
Aprotected
Bpublic
Cinternal
Dprivate
What does the internal modifier restrict visibility to?
AOnly the containing class
BThe same module
CAnywhere in the project
DOnly subclasses
Which modifier allows visibility in subclasses but not outside the class hierarchy?
Aprotected
Bpublic
Cprivate
Dinternal
If no visibility modifier is specified in Kotlin, what is the default?
Apublic
Binternal
Cprotected
Dprivate
Can a private member be accessed from another file in the same module?
AYes
BOnly if it is a subclass
CNo
DOnly if marked internal
Describe the four visibility modifiers in Kotlin and give a simple example of when to use each.
Think about who should see your code: everyone, only inside, inside module, or subclasses.
You got /5 concepts.
    Explain why you might choose internal over public for a Kotlin class.
    Consider controlling access in bigger projects with multiple modules.
    You got /4 concepts.