Nested and Inner Classes in Kotlin

In Kotlin, classes can be defined inside other classes to logically group and encapsulate related functionality. These are known as nested and inner classes. Although they look similar, they behave differently in how they access members of the outer class.

1. Why Use Nested or Inner Classes?

Nested and inner classes improve structure and encapsulation:

2. Nested Classes

A nested class is simply a class defined inside another class. By default, it does not hold a reference to the outer class and cannot access its members.


class Outer {
    private val outerName = "Outer Class"

    class Nested {
        fun show() = "Hello from Nested Class"
        // Cannot access outerName directly
    }
}

fun main() {
    val nested = Outer.Nested() // No need to create Outer instance
    println(nested.show())
}
  

Output:

Hello from Nested Class

Key Points:

3. Inner Classes

If you want the nested class to access the members of the outer class, you must declare it as an inner class using the inner keyword.


class Outer {
    private val outerName = "Outer Class"

    inner class Inner {
        fun show() = "Accessing: $outerName from Inner Class"
    }
}

fun main() {
    val inner = Outer().Inner() // Requires Outer instance
    println(inner.show())
}
  

Output:

Accessing: Outer Class from Inner Class

Key Points:

4. Accessing Outer Class Members

The this@OuterClassName syntax allows inner classes to explicitly refer to the outer class instance.


class Outer {
    private val outerName = "Outer"

    inner class Inner {
        private val innerName = "Inner"
        fun display() {
            println("Inner Name: $innerName")
            println("Outer Name: ${this@Outer.outerName}")
        }
    }
}

fun main() {
    val obj = Outer().Inner()
    obj.display()
}
  

Output:


Inner Name: Inner
Outer Name: Outer
  

5. Nested vs Inner Class (Comparison)

Feature Nested Class Inner Class
Keyword Defined normally Declared using inner
Outer Class Reference Does not hold a reference Holds a reference
Access to Outer Members Cannot access outer members Can access outer members (even private ones)
Object Creation Outer.Nested() Outer().Inner()
Analogy in Java Static nested class Non-static inner class

6. Nested and Inner Classes in Real Use Cases

7. Technical Interview Questions

8. Key Takeaways