Kotlin Tutoial

Basics

Control Flow

Array & String

Functions

Collections

OOPs Concept

Exception Handling

Null Safety

Regex & Ranges

Java Interoperability

Miscellaneous

Android

Kotlin Nested class and Inner class

In Kotlin, like many other languages, you can define a class within another class. Such classes are categorized as either nested classes or inner classes. Let's explore the differences and how to use each.

1. Nested Classes

By default, a class defined within another class is called a nested class. It's static and does not have access to the outer class's members.

Example:

class Outer {
    private val outerVar = "I'm in the Outer class"

    class Nested {
        fun printMessage() {
            // Cannot access 'outerVar' here
            println("I'm in the Nested class")
        }
    }
}

fun main() {
    val nestedInstance = Outer.Nested()
    nestedInstance.printMessage()  // prints "I'm in the Nested class"
}

In this example, the Nested class cannot access the members of the Outer class.

2. Inner Classes

If you want the inner class to have access to the outer class's members, you mark it with the inner keyword. This type of class keeps a reference to an object of an outer class.

Example:

class Outer {
    private val outerVar = "I'm in the Outer class"

    inner class Inner {
        fun printMessage() {
            println("I'm in the Inner class and $outerVar")
        }
    }
}

fun main() {
    val innerInstance = Outer().Inner()
    innerInstance.printMessage()  // prints "I'm in the Inner class and I'm in the Outer class"
}

In this case, the Inner class can access the members of the Outer class because it's marked with the inner keyword.

Differences:

  • Accessibility: Nested classes cannot access the outer class's members, while inner classes can.

  • Reference: Nested classes don't hold a reference to an instance of the outer class, while inner classes do. This difference can have implications on memory management and can lead to memory leaks if not handled properly (e.g., in contexts like Android development).

  • Initialization: For nested classes, you don't need an instance of the outer class to create an instance of the nested class. But for inner classes, you do need an instance of the outer class.

Summary:

Nested and inner classes are powerful tools that allow for better organization and encapsulation in your Kotlin programs. Use nested classes when you want a static class that doesn't require access to the outer class's properties or functions. Use inner classes when you want a non-static class that can access and modify the outer class's properties and functions.

  1. Defining nested classes in Kotlin:

    • Nested classes are defined within the scope of another class.
    class OuterClass {
        class NestedClass {
            // Nested class definition
        }
    }
    
  2. Accessing outer class members from a nested class in Kotlin:

    • Nested classes don't have access to the outer class members by default.
    class OuterClass {
        class NestedClass {
            // Cannot access members of OuterClass directly
        }
    }
    
  3. Visibility modifiers in Kotlin nested classes:

    • Nested classes have default package-level visibility. You can use modifiers like private or internal.
    class OuterClass {
        private class NestedClass {
            // private nested class
        }
    }
    
  4. Creating instances of nested classes in Kotlin:

    • Instances of nested classes can be created without an instance of the outer class.
    val nestedInstance = OuterClass.NestedClass()
    
  5. Declaring inner classes in Kotlin:

    • Inner classes are defined within the scope of another class and have access to its members.
    class OuterClass {
        inner class InnerClass {
            // Inner class definition
        }
    }
    
  6. Accessing outer class members from an inner class in Kotlin:

    • Inner classes have access to the members of the outer class.
    class OuterClass {
        inner class InnerClass {
            fun accessOuterMember() {
                // Access members of OuterClass here
            }
        }
    }
    
  7. Visibility modifiers in Kotlin inner classes:

    • Inner classes have the same visibility as nested classes but also have access to the outer class's private members.
    class OuterClass {
        private inner class InnerClass {
            // private inner class with access to outer class's members
        }
    }
    
  8. Creating instances of inner classes in Kotlin:

    • Instances of inner classes require an instance of the outer class.
    val outerInstance = OuterClass()
    val innerInstance = outerInstance.InnerClass()