Kotlin Nested and Inner Class: In this tutorial, you will learn to function with nested and inner classes with the assistance of examples.
In this article, you will learn-
Kotlin Nested Class
Nested class is such class which is created inside another class. In Kotlin, nested class is by default static, so its data member and member function can be accessed to without creating an object of class. Nested class can’t have the option to access to the data member from outer class.
Comparable like Java, Kotlin allows you to characterize a class inside another class known as nested class.
class Outer { ... .. ... class Nested { ... .. ... } }
Since the Nested class is an individual from its enclosing class Outer, you can use it. the notation to access to Nested class and its members.
Example: Kotlin Nested Class
class Outer { val a = "Outside Nested class." class Nested { val b = "Inside Nested class." fun callMe() = "Function call from inside Nested class." } } fun main(args: Array<String>) { // accessing member of Nested class println(Outer.Nested().b) // creating object of Nested class val nested = Outer.Nested() println(nested.callMe()) }
At the point when you run the program, the output will be:
Inside Nested class. Function call from inside Nested class.
For Java Users
The nested class in Kotlin is like static nested class in Java.
In Java, when you declare a class inside another class, it turns into an inner class by default. Anyway in Kotlin, you need to use the inner modifier to create an inner class which we will discuss next.
Kotlin Inner Class
Inner class is a class which is created inside another class with watchword inner. As such, we can say that a nested class which is marked as “inner” is called inner class.
Inner class can’t be declared inside interfaces or non-inner nested classes.
The nested classes in Kotlin don’t approach the external class instance. For example,,
class Outer { val foo = "Outside Nested class." class Nested { // Error! cannot access member of outer class. fun callMe() = foo } } fun main(args: Array<String>) { val outer = Outer() println(outer.Nested().callMe()) }
The above code will not compile since we attempted to access to foo property of Outer class from inside Nested class.
To order this issue, you need to mark the nested class with inner to create an inner class. Inner classes convey a reference to an external class and can access external class members.
Example: Kotlin Inner Class
class Outer { val a = "Outside Nested class." inner class Inner { fun callMe() = a } } fun main(args: Array<String>) { val outer = Outer() println("Using outer object: ${outer.Inner().callMe()}") val inner = Outer().Inner() println("Using inner object: ${inner.callMe()}") }
At the point when you run the program, the output will be:
Using outer object: Outside Nested class. Using inner object: Outside Nested class.
Recommended Reading: Anonymous Inner Class
Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.