A nested class is a class defined inside another class. In Kotlin, it does not hold a reference to the outer class by default.
Click to reveal answer
beginner
How does a nested class differ from an inner class in Kotlin?
A nested class is independent and cannot access members of the outer class. An inner class holds a reference to the outer class and can access its members.
Click to reveal answer
beginner
Can a nested class access properties of its outer class in Kotlin?
No, a nested class cannot access properties or functions of the outer class unless it is marked as inner.
Click to reveal answer
intermediate
Why might you use a nested class instead of an inner class?
Use a nested class when you want to group a class logically inside another but keep it independent, avoiding unnecessary references to the outer class.
Click to reveal answer
intermediate
Show a simple Kotlin example of a nested class and explain its independence.
class Outer {
class Nested {
fun greet() = "Hello from Nested"
}
}
// Usage:
val nested = Outer.Nested()
println(nested.greet())
// Explanation: Nested class does not access Outer members and can be created without an Outer instance.
Click to reveal answer
In Kotlin, what does a nested class NOT have by default?
AIts own properties and methods
BAbility to be instantiated
CReference to the outer class instance
DA name
✗ Incorrect
A nested class does not hold a reference to the outer class instance unless marked as inner.
Which keyword makes a nested class able to access outer class members?
Ainner
Bopen
Coverride
Dcompanion
✗ Incorrect
The 'inner' keyword allows the nested class to access members of the outer class.
How do you create an instance of a nested class in Kotlin?
Aval obj = Outer().Nested()
Bval obj = Outer.Nested()
Cval obj = Nested()
Dval obj = Outer.Inner()
✗ Incorrect
A nested class instance is created using Outer.Nested() without needing an outer class instance.
Which statement is true about nested classes in Kotlin?
AThey cannot have constructors.
BThey can access private members of the outer class.
CThey must be marked with 'static' keyword.
DThey are static by default.
✗ Incorrect
Nested classes in Kotlin behave like static classes in Java, meaning they do not hold an implicit reference to the outer class.
Why might you avoid using inner classes when not needed?
AThey increase memory usage by holding outer class references.
BThey cannot access outer class members.
CThey cannot be instantiated.
DThey are slower to compile.
✗ Incorrect
Inner classes hold a reference to the outer class, which can increase memory usage if not necessary.
Explain the difference between a nested class and an inner class in Kotlin.
Think about how each class relates to the outer class and what members they can access.
You got /4 concepts.
Describe a situation where using a nested class is better than an inner class.
Consider memory and design simplicity.
You got /4 concepts.
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.