Complete the code to declare a nested class inside Outer.
class Outer { class [1] { fun greet() = "Hello from Nested" } }
The keyword class Nested declares a nested class inside Outer. Nested classes do not have access to the outer class instance.
Complete the code to create an instance of the nested class.
val nested = Outer.[1]()To create an instance of a nested class, use Outer.Nested(). Nested classes do not require an outer instance.
Fix the error in accessing the outer class property from the nested class.
class Outer(val name: String) { class Nested { fun printName() = [1] } }
Nested classes do not have access to the outer class instance or its properties. So you cannot access name directly. The correct approach is to pass the value explicitly if needed.
Fill both blanks to create an inner class that can access the outer class property.
class Outer(val name: String) { inner class [1] { fun printName() = [2] } }
Using inner class Inner allows access to the outer class instance. Inside the inner class, this@Outer.name accesses the outer property.
Fill all three blanks to create a nested class with a function that returns a greeting including a passed outer class name.
class Outer { class [1] { fun greet(name: String) = "Hello, [2]!" } } val nested = Outer.[3]() println(nested.greet("Alice"))
The nested class is named 'Nested'. The greet function uses string interpolation with $name to include the passed name. The instance is created with Outer.Nested().