Visibility modifiers (public, private, internal, protected) in Kotlin - Time & Space Complexity
Let's explore how the use of visibility modifiers affects the time it takes for Kotlin code to run.
We want to see if changing visibility changes how long the program takes as it grows.
Analyze the time complexity of the following Kotlin class with different visibility modifiers.
class Example {
private val secretData = List(1000) { it * 2 }
fun getData(): List {
return secretData.filter { it % 3 == 0 }
}
}
This code creates a list and filters it using a public function, while the list itself is private.
Look for loops or repeated actions in the code.
- Primary operation: Filtering the list with
filterwhich checks each item. - How many times: Once per call, it checks all 1000 items in the list.
As the list size grows, the filtering takes longer because it looks at each item.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the list size; double the list, double the work.
Time Complexity: O(n)
This means the time to run grows in a straight line with the size of the list being filtered.
[X] Wrong: "Making a variable private makes the code run faster."
[OK] Correct: Visibility controls who can use the code, not how fast it runs. The time depends on what the code does, not who can see it.
Understanding visibility modifiers helps you write clear and safe code. Knowing they don't affect speed shows you focus on what really matters when coding.
"What if the filter function was called multiple times instead of once? How would the time complexity change?"
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
