Nested classes in Kotlin are like small boxes inside a bigger box. They work on their own and don't need the bigger box to do their job.
When you want to group related classes together but keep them separate in behavior.
When the inner class does not need to access the outer class's properties or functions.
When you want to organize code clearly without mixing responsibilities.
When you want to avoid accidental access to outer class members from the nested class.
Syntax
Kotlin
class Outer {
class Nested {
fun greet() = "Hello from Nested"
}
}
A nested class in Kotlin is declared with the class keyword inside another class.
By default, nested classes do not have access to the outer class's members.
Examples
The nested class Nested cannot access outerName directly.
Kotlin
class Outer {
val outerName = "Outer"class Nested {
fun nestedGreet() = "Hi from Nested"
}
}
An inner class can access the outer class's properties, unlike a nested class.
Kotlin
class Outer {
val outerName = "Outer"
inner class Inner {
fun innerGreet() = "Hi from $outerName"
}
}
You create an instance of a nested class using Outer.Nested() without needing an outer class instance.
Kotlin
fun main() {
val nested = Outer.Nested()
println(nested.nestedGreet())
}
Sample Program
This program shows that the nested class works independently and prints its own message.
Kotlin
class Outer {
val outerMessage = "Outer class message"class Nested {
fun nestedMessage() = "Nested class message"
}
}
fun main() {
val nested = Outer.Nested()
println(nested.nestedMessage())
// The nested class cannot access outerMessage directly
}
OutputSuccess
Important Notes
Remember, nested classes do not hold a reference to the outer class instance.
If you want the inner class to access outer class members, use the inner keyword.
Summary
Nested classes are independent and do not access outer class members.
They help organize code without mixing responsibilities.
Use inner classes if you need access to outer class properties.
Practice
(1/5)
1. What is true about a nested class in Kotlin compared to an inner class?
easy
A. A nested class can access all members of the outer class directly.
B. A nested class does not have access to the outer class's members.
C. A nested class is the same as an inner class.
D. A nested class must be declared with the inner keyword.
Solution
Step 1: Understand nested class independence
In Kotlin, a nested class is static by default and cannot access outer class members.
Step 2: Compare with inner class
Only inner classes (declared with inner) can access outer class properties and functions.
Final Answer:
A nested class does not have access to the outer class's members. -> Option B
Quick Check:
Nested class independence = A [OK]
Hint: Remember: nested = no outer access, inner = has outer access [OK]
Common Mistakes:
Confusing nested and inner classes
Thinking nested classes access outer members
Assuming nested classes require 'inner' keyword
2. Which of the following is the correct syntax to declare a nested class inside an outer class in Kotlin?
easy
A. class Outer { inner class Nested { } }
B. class Outer { nested class Nested { } }
C. class Outer { static class Nested { } }
D. class Outer { class Nested { } }
Solution
Step 1: Recall Kotlin nested class syntax
In Kotlin, nested classes are declared simply inside the outer class without any keyword.
Step 2: Identify incorrect options
inner declares an inner class, static and nested keywords do not exist in Kotlin.
Final Answer:
class Outer { class Nested { } } -> Option D
Quick Check:
Nested class syntax = B [OK]
Hint: Nested class = just class inside class, no keywords needed [OK]
Common Mistakes:
Using 'static' keyword (Java style)
Using 'nested' keyword (not valid Kotlin)
Confusing inner and nested class syntax
3. What will be the output of this Kotlin code?
class Outer {
val x = 10
class Nested {
fun printX() = "Cannot access x"
}
}
fun main() {
val nested = Outer.Nested()
println(nested.printX())
}
medium
A. Cannot access x
B. NullPointerException
C. Compilation error
D. 10
Solution
Step 1: Analyze nested class access
The nested class cannot access the outer class property x because it is not an inner class.
Step 2: Check printX() function
It returns the string "Cannot access x" explicitly, so that will be printed.