kotlin
  1. kotlin-nested-and-inner-class

Kotlin Nested and Inner Class

In Kotlin, you can define classes and interfaces within another class or interface. These classes and interfaces are known as nested classes. In addition, Kotlin also provides inner classes, which are similar to nested classes but have access to the members of the outer class. In this tutorial, we will explore nested and inner classes in Kotlin.

Syntax

To define a nested class or interface in Kotlin, use the following syntax:

class Outer {
    // outer class members

    class Nested {
        // nested class members
    }
}

interface OuterInterface {
    // outer interface members
        
    class Nested {
        // nested class members
    }
}

To define an inner class, use the "inner" keyword as follows:

class Outer {
    // outer class members

    inner class Inner {
        // inner class members can access the outer class members
    }
}

Example

The following example demonstrates how to define nested and inner classes in Kotlin:

class Outer {
    private val outerName: String = "Outer class"

    class Nested {
        fun nestedMethod() {
            println("This is a nested class!")
        }
    }

    inner class Inner {
        fun innerMethod() {
            println("This is an inner class with access to ${outerName}!")
        }
    }
}

fun main() {
    val nestedObj = Outer.Nested()
    nestedObj.nestedMethod()
    
    val outerObj = Outer()
    val innerObj = outerObj.Inner()
    innerObj.innerMethod()
}

Output

This is a nested class!
This is an inner class with access to Outer class!

Explanation

In the above example, we have defined the Outer class with a private member outerName. We have then defined a nested class Nested that has a function nestedMethod which simply prints a message. We have also defined an inner class Inner that has a function innerMethod that can access the private member outerName.

In the main function, we create an instance of the Nested class using the syntax Outer.Nested(), which is possible because the nested class is defined as a static class. We then call the nestedMethod function on this instance.

Next, we create an instance of the Outer class using val outerObj = Outer(). We can then create an instance of the Inner class using val innerObj = outerObj.Inner(), which is possible because the inner class is defined as a non-static class and can access the members of the outer class. We then call the innerMethod function on this instance.

Use

Nested and inner classes in Kotlin can be used to organize code and make it more readable. Nested classes can be used to encapsulate functionality that is specific to the outer class. Inner classes provide a way to access the members of the outer class from within the inner class.

Important Points

  • Use the "class" keyword to define a nested class or interface.
  • Use the "inner" keyword to define an inner class.
  • A nested class is a static class and does not have access to the members of the outer class.
  • An inner class is a non-static class and has access to the members of the outer class.

Summary

In this tutorial, we discussed nested and inner classes in Kotlin. We covered the syntax, example, output, explanation, use, and important points of nested and inner classes in Kotlin. With this knowledge, you can now define nested and inner classes in your Kotlin programs and organize your code more effectively.

Published on: