📖 Scenario: Imagine you are creating a simple app to manage books and their details. You want to organize your code by using a nested class inside a main class. This project will help you understand how a nested class in Kotlin works independently from its outer class.
🎯 Goal: You will create a Kotlin program with an outer class Book and a nested class Details. You will see how the nested class can be used without needing an instance of the outer class.
📋 What You'll Learn
Create an outer class called Book with a property title of type String.
Create a nested class inside Book called Details with a property author of type String.
Create an instance of the nested class Details without creating an instance of Book.
Print the author property of the nested class instance.
💡 Why This Matters
🌍 Real World
Nested classes help organize related code together, making it easier to manage and understand complex programs.
💼 Career
Understanding nested classes is useful for Kotlin developers working on Android apps or backend services where clean code structure is important.
Progress0 / 4 steps
1
Create the outer class Book with a title property
Create a class called Book with a property title of type String initialized to "Kotlin Programming".
Kotlin
Hint
Use class Book to define the class and val title: String = "Kotlin Programming" to create the property.
2
Add a nested class Details with an author property
Inside the Book class, create a nested class called Details with a property author of type String initialized to "John Doe".
Kotlin
Hint
Define the nested class with class Details inside Book. Add the author property inside it.
3
Create an instance of the nested class Details without an outer class instance
Create a variable called details and assign it a new instance of the nested class Book.Details() without creating an instance of Book.
Kotlin
Hint
Use Book.Details() to create the nested class instance without an outer class object.
4
Print the author property of the nested class instance
Write a println statement to display the author property of the details instance.
Kotlin
Hint
Use println(details.author) to print the author name.
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.