Inner classes are a powerful feature in Kotlin that allow you to define a class within another class. They provide a way to logically group classes that are only used in one place, increasing encapsulation and creating cleaner, more readable code.
An inner class in Kotlin is a class that is nested inside another class and has access to the outer class's members. Unlike nested classes, inner classes maintain a reference to their outer class instance.
To define an inner class in Kotlin, use the inner
keyword before the class
keyword. Here's a basic example:
class OuterClass {
private val outerProperty = "Outer Property"
inner class InnerClass {
fun accessOuterProperty() {
println(outerProperty)
}
}
}
In this example, InnerClass
can access the private property outerProperty
of OuterClass
.
To create an instance of an inner class, you need an instance of the outer class. The syntax is as follows:
val outer = OuterClass()
val inner = outer.InnerClass()
inner.accessOuterProperty() // Prints: Outer Property
Inner classes are particularly useful in scenarios where you need:
A common use case for inner classes is implementing event listeners. Here's an example:
class Button(private val label: String) {
private var clickListener: OnClickListener? = null
fun setOnClickListener(listener: OnClickListener) {
clickListener = listener
}
fun click() {
clickListener?.onClick(this)
}
interface OnClickListener {
fun onClick(button: Button)
}
inner class ButtonClickListener : OnClickListener {
override fun onClick(button: Button) {
println("${button.label} was clicked!")
}
}
}
fun main() {
val button = Button("Submit")
button.setOnClickListener(button.ButtonClickListener())
button.click() // Prints: Submit was clicked!
}
In this example, ButtonClickListener
is an inner class that implements the OnClickListener
interface. It has access to the outer class's label
property, demonstrating the tight coupling between inner and outer classes.
By understanding and utilizing inner classes effectively, you can create more organized and encapsulated code in your Kotlin projects. They provide a powerful tool for structuring related classes and implementing complex behaviors within a larger class context.