Blog / April 22, 2023 / 3 mins read / By Suneet Agrawal

Nested Class vs Inner Class : Kotlin

Nested and Inner classes are two important concepts in Kotlin that allow developers to organize and structure their code in a more effective way. In this blog, we’ll explore these concepts and understand how they can be used in Kotlin.

Nested Classes

A nested class is a class that is defined inside another class. It is also known as a static inner class, as it is not tied to any specific instance of the outer class. This means that a nested class can be accessed without creating an instance of the outer class.

To define a nested class in Kotlin, we use the class keyword inside the body of the outer class.

For Example, to defined a nested class called NestedClass inside the OuterClass,

class OuterClass {
    class NestedClass {
        fun doSomething(){
            //I'll do something
        }
    }
}

Note that the nested class cannot access the properties and methods of the outer class directly.

class OuterClass {
    private val outerProperty = "Outer property"
    
    class NestedClass {
        fun printOuterProperty() {
            <e>// This won't compile because the nested class cannot access the outer property</e>
            // println(outerProperty)
        }
    }
}

If we try to access the outerProperty inside the printOuterProperty() method, it will result in a compilation error.

Inner Classes

An inner class is a class that is defined inside another class, but it has access to the properties and methods of the outer class. This means that an instance of the inner class is tied to an instance of the outer class. In Kotlin, we use the inner keyword to define an inner class.

class OuterClass {
    private val outerProperty = "Outer property"
    
    inner class InnerClass {
        fun printOuterProperty() {
            // We can access the outer property because this is an inner class
            println(outerProperty)
        }
    }
}

In the example above, we have defined an inner class called InnerClass inside the OuterClass. Note that we have used the inner keyword to define the inner class. Inside the printOuterProperty() method, we can access the outerProperty because this is an inner class.

Nested vs Inner Classes

  • The key difference between nested and inner classes is the access to the properties and methods of the outer class. A nested class cannot access the properties and methods of the outer class directly, while an inner class can.
  • Another difference is that a nested class is static, while an inner class is tied to an instance of the outer class.

When to use Nested and Inner Classes

Nested and inner classes can be used in different scenarios based on their differences. A nested class is useful when we want to group related classes together, but the nested class doesn’t need access to the properties and methods of the outer class. On the other hand, an inner class is useful when we want to create a class that is closely related to the outer class and needs access to its properties and methods.

Conclusion

A nested class is a class that is defined inside another class and is static, while an inner class is a class that is defined inside another class and has access to its properties and methods. We can use nested classes to group related classes together, while we use inner classes to create classes that are closely related to the outer class.

Comments