What if your code had secret rooms only trusted friends could enter?
Why Visibility modifiers (public, private, internal, protected) in Kotlin? - Purpose & Use Cases
Imagine you are building a big house with many rooms, but you want to keep some rooms locked so only certain people can enter. Without locks, anyone can wander everywhere, causing confusion and mistakes.
Without visibility controls, all parts of your code are open to everyone. This means anyone can change or use things they shouldn't, leading to bugs and security problems. It's like leaving all your doors wide open.
Visibility modifiers act like locks on your code's rooms. They let you decide who can see or use each part, keeping your code safe, organized, and easier to manage.
class BankAccount { var balance = 0 } // Anyone can change balance directly
class BankAccount { private var balance = 0 fun deposit(amount: Int) { balance += amount } } // balance is protected inside the class
It lets you protect important parts of your code so only the right pieces can access or change them, making your programs safer and easier to fix.
Think of a bank app where the user can deposit money but cannot directly change the balance number. Visibility modifiers keep the balance hidden and safe inside the app.
Visibility modifiers control who can see or use parts of your code.
They prevent accidental or harmful changes by restricting access.
This leads to safer, cleaner, and more reliable programs.
Practice
Solution
Step 1: Understand the meaning of
Thepublicpublicmodifier means the member is visible everywhere in the project.Step 2: Compare with other modifiers
privaterestricts to class/file,internalrestricts to module,protectedrestricts to class and subclasses only.Final Answer:
public -> Option CQuick Check:
Visible everywhere = public [OK]
- Confusing private with public visibility
- Thinking internal is visible everywhere
- Assuming protected is public
count in a Kotlin class?Solution
Step 1: Recall Kotlin property declaration syntax
Visibility modifier comes beforevalorvar, soprivate val count: Int = 0is correct.Step 2: Check other options for syntax errors
Options B, C, and D have incorrect order of keywords, which is invalid in Kotlin.Final Answer:
private val count: Int = 0 -> Option AQuick Check:
Modifier before val/var = correct syntax [OK]
- Placing private after val
- Mixing order of keywords
- Omitting colon before type
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?
Solution
Step 1: Understand
protectedvisibilityprotectedmembers are accessible inside the class and its subclasses, but not outside.Step 2: Analyze access in
Accessingmainfunctionchild.secretfrom outside the class hierarchy causes a compilation error.Final Answer:
Compilation error: Cannot access 'secret' outside the class or subclass -> Option AQuick Check:
Protected blocks external access [OK]
- Assuming protected is public
- Expecting runtime error instead of compile error
- Confusing private and protected
class Example {
private val secret = "top secret"
}
fun main() {
val ex = Example()
println(ex.secret)
}What is the error and how to fix it?
Solution
Step 1: Identify visibility of
The propertysecretsecretis declaredprivate, so it cannot be accessed outside the class.Step 2: Understand the error cause and fix
Trying to printex.secretfrom outside causes a compilation error. Changingprivatetopublicor providing a public method to access it fixes the error.Final Answer:
Error: Cannot access private property outside class; fix by changing to public -> Option DQuick Check:
Private blocks external access [OK]
- Thinking private allows outside access
- Confusing syntax errors with visibility errors
- Ignoring need for accessor methods
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?Solution
Step 1: Understand
internalvisibilityinternalmeans visible anywhere inside the same module, regardless of file.Step 2: Apply to given files
SinceFileA.ktandFileB.ktare in the same module,greet()is accessible fromFileB.kt.Final Answer:
greet()can be called fromFileB.ktbecauseinternalallows access within the same module -> Option BQuick Check:
Internal = visible in module [OK]
- Confusing internal with private or file-private
- Thinking internal restricts to one file
- Assuming inheritance needed for internal access
Ready to prove it?
Pass the test and this pattern moves unknown
