Kotlin Nested class and Inner class

Nested Class

A class is declared within another class then it is called as nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating object of the class.

Syntax of declaration:

class outerClass {
       ............
      // outer class properties or member function
      
      class nestedClass { 
            ..........
            // inner class properties or member function
      }
}

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



Koltin program of accessing nested class properties –

filter_none

edit
close

play_arrow

link
brightness_4
code

// outer class declaration
class outerClass {
    var str = "Outer class"
    // nested class declaration
    class nestedClass {
        val firstName  = "Praveen"
        val lastName = "Ruhil"
    }
}
fun main(args: Array<String>) {
    // accessing member of Nested class
    print(outerClass.nestedClass().firstName)
    print(" ")
    println(outerClass.nestedClass().lastName)
}    

chevron_right


Output:

Praveen Ruhil

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

Koltin program of accessing nested class member function –

filter_none

edit
close

play_arrow

link
brightness_4
code

//outer class declaration
class outerClass {
    var str = "Outer class"
    // nested class declaration
    class nestedClass {
        var s1 = "Nested class"
        // nested class member function
        fun nestfunc(str2: String): String {
            var s2 = s1.plus(str2)
            return s2
        }
    }
}
fun main(args: Array<String>) {
  
    // creating object of Nested class
    val nested = outerClass.nestedClass()
    // invoking the nested member function by passing string 
    var result = nested.nestfunc(" member function call successful")
    println(result)
}

chevron_right


Output:

Nested class member function call successful

Comparison with Java –

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

Kotlin Inner Class –

When we can declare a class inside another class using the keyword inner then it is called inner class. With the help of inner class we can access the outer class property inside the inner class.

class outerClass {
       ............
      // outer class properties or member function
      
      inner class innerClass { 
            ..........
            // inner class properties or member function
      }
}

In the below program we are trying to access str from the inner class member function. But it does not work and gives compile time error.
Koltin program of inner class –

filter_none

edit
close

play_arrow

link
brightness_4
code

// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration without using inner keyword
    class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can not access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array<String>) {
    // creating object for inner class
    val inner= outerClass.innerClass()
    // inner function call using object
    println(inner.nestfunc())
}

chevron_right


Output:

Error:(9, 22) Kotlin: Unresolved reference: str

First, use inner keyword in front of inner class. Then, create an instance of the outer class else we can’t use inner classes.

filter_none

edit
close

play_arrow

link
brightness_4
code

// outer class declaration
class outerClass {
    var str = "Outer class"
    // innerClass declaration with using inner keyword
    inner class innerClass {
        var s1 = "Inner class"
        fun nestfunc(): String {
            // can access the outer class property str
            var s2 = str
            return s2
        }
    }
}
// main function
fun main(args: Array<String>) {
    // creating object for inner class
    val inner= outerClass().innerClass()
    // inner function call using object
    println(inner.nestfunc()+" property accessed successfully from inner class ")
}

chevron_right


Output:

Outer class property accessed successfully from inner class


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.