Open In App

Kotlin Nested class and Inner class

Last Updated : 25 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, you can define a class inside another class. Such classes are categorized as either nested classes or inner classes, each with different behavior and access rules.

Nested Class

A nested class is a class declared inside another class without the inner keyword. By default, a nested class does not have access to the members (fields or methods) of the outer class. This is similar to static nested classes in Java.

Syntax: 

class OuterClass {
// Outer class members

class NestedClass {
// Nested class members
}
}

Accessing Nested Class -

Nested class can't access the members of the outer class, but we can access the property of nested class from the outer class without creating an object for nested class.

Example: 

Kotlin
class Engine {
    class Specs {
        fun showSpecs() {
            println("250 horsepower, 6 cylinders")
        }
    }
}

fun main() {
    val specs = Engine.Specs()
    specs.showSpecs()
}

Output: 

250 horsepower, 6 cylinders

In Kotlin, to access the member function of nested class, we need to create the object for nested class and call the member function using it.

Comparison with Java

Kotlin classes are much similar to Java classes when we think about the capabilities and use cases, but not identical. Nested in Kotlin is similar to a static nested class in Java and the Inner class is similar to a non-static nested class in Java.  

Inner Class

An inner class is a nested class marked with the inner keyword. Unlike nested classes, inner classes maintain a reference to their outer class and can access its members.

Example:

Kotlin
class OuterClass {
    private val message = "Outer class property"

    inner class InnerClass {
        fun showMessage() {
            println(message) // Can access outer class property
        }
    }
}


Accessing Inner Class -

We need an instance of the outer class to create an instance of the inner class.

Example:

Kotlin
fun main() {
    val outer = OuterClass()
    val inner = outer.InnerClass()
    inner.showMessage()
}

Output: 

Outer class property

Important -

If we try to access an outer class property from an inner class without using the inner keyword, it will result in a compile-time error

Example:

Kotlin
class OuterClass {
    private val str = "Hello"

    class InvalidInnerClass {
        fun show() {
            println(str)
        }
    }
}


Output:

Error: Unresolved reference 'str'.

Advantages:

  1. Encapsulation: Nested and inner classes allow you to group related functionality together and keep it separate from the rest of the code, improving code organization and readability.
  2. Reusability: Nested and inner classes can be reused within the same class or across multiple classes, making it easier to write more maintainable code.
  3. Accessibility: Inner classes have access to the members of the outer class, making it easier to share data between them and reducing code duplication.

Disadvantages:

  1. Complexity: Adding nested and inner classes can make the code more complex and harder to understand, especially if there are multiple levels of nesting or if inner classes are used excessively.
  2. Performance: Using nested and inner classes can slow down the performance of your code, especially if they are heavily used or if they are nested to many levels deep.
  3. Debugging: Debugging nested and inner classes can be challenging, especially if there are multiple levels of nesting or if inner classes are used excessively.
  4. Overall, it's important to use nested and inner classes in a balanced and appropriate way to take advantage of their benefits while avoiding their disadvantages.

Next Article
Article Tags :

Similar Reads