Interview prep · Stage 1: Kotlin and DSA basics

Kotlin inner vs nested class interview questions

Mid · 5 questions · 3 min read

One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.

Try each question out loud before you open the answer.

Opening question

What is the difference between a nested class and an inner class in Kotlin?

Show answer

Junior answer

An inner class can use the outer class's members. A nested class can't.

Senior answer

A nested class has no link to any outer object. It compiles to a static nested class, and you create it with Outer.Nested(). An inner class belongs to one outer object: the compiler adds a hidden this$0 field, you create it with outer.Inner(), and inside it this@Outer reads that field. Kotlin makes nested the default; Java does the opposite.

What they're really checking: That you know where the access comes from: a hidden reference, not magic. That reference is also the cost.

Follow-up 1

This doesn't compile. Why, and what are two ways to fix it?

Show answer
class OrderTracker(val orderId: String) {
    class DeliveryStop(val name: String) {
        fun label() = "$name for order $orderId"   // error
    }
}

Answer

DeliveryStop is nested, so it has no OrderTracker object to read orderId from. The error is "Outer class ... of non-inner class cannot be used as receiver". Fix 1: mark it inner, then create it with tracker.DeliveryStop(...). Fix 2, often better: keep it nested and pass orderId in as a constructor parameter, so it doesn't hold the whole tracker.

What they're really checking: Whether you reach for inner by reflex, or ask if the class really needs the whole outer object.

Follow-up 2

What does the compiler generate for an inner class?

Show answer

Junior answer

A reference to the outer class.

Senior answer

A non-static class with a field this$0 (package-private and synthetic) that points to the outer object. The constructor gets the outer object as an extra first parameter, so outer.Inner() becomes new Inner(outer). If the inner class uses a private outer member, Kotlin also adds synthetic access$ helper methods in the outer class. You can see all of it in Android Studio: Tools > Kotlin > Show Kotlin Bytecode > Decompile.

What they're really checking: Reading the bytecode, not just the docs. Senior candidates can describe the constructor change.

Follow-up 3

Does this leak the Activity? Would making StatusUpdater a nested class fix it?

Show answer
object OrderUpdates {
    private val listeners = mutableListOf<StatusListener>()
    fun add(listener: StatusListener) { listeners += listener }
    fun remove(listener: StatusListener) { listeners -= listener }
}

class OrderTrackingActivity : AppCompatActivity() {
    private lateinit var statusText: TextView

    inner class StatusUpdater : StatusListener {
        override fun onStatus(status: String) { statusText.text = status }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        OrderUpdates.add(StatusUpdater())
    }
}

Answer

Yes, it leaks. OrderUpdates lives as long as the app, it holds the listener, and the listener's this$0 holds the Activity and its views. Every rotation leaves one more Activity in memory. Making it nested and passing statusText doesn't fix it, because a View holds its Context, which is the Activity. The fix is the lifetime: add the listener in onStart and remove it in onStop, or expose a Flow and collect it with repeatOnLifecycle.

What they're really checking: "Use a nested class" is the textbook answer. The real question is whether the object outlives its outer object.

Follow-up 4

Does every lambda or object : expression hold the outer class?

Show answer
class Screen(val title: String) {
    fun quiet() = object : Runnable {
        override fun run() { println("hi") }       // no this$0
    }
    fun capturing() = object : Runnable {
        override fun run() { println(title) }      // holds Screen
    }
}

Junior answer

Yes, like an anonymous class in Java.

Senior answer

No. In Kotlin, a lambda or an object : expression only captures the outer object if its body uses something from it. Here the first Runnable gets no this$0 field; the second one does, because it reads title. One stray property inside a callback is enough to pull the whole Activity in.

What they're really checking: Knowing Kotlin differs from old Java anonymous classes, and that capture depends on what the body uses.

Say it in 30 seconds

Show the answer

A nested class has no link to an outer object; it compiles to a static nested class. An inner class keeps a hidden this$0 reference to its outer object, which lets it use outer members but also keeps the outer object alive. That's how an inner listener given to a long-lived object leaks an Activity. The fix is matching lifetimes, for example removing the listener in onStop.

How did you do?