65
Kotlin Nested class and Inner class
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 without creating an object of class. Nested class cannot be able to access the data member of outer class.
Kotlin Nested Class Example
Output:
code inside nested class Id is 101
Kotlin Inner class
Inner class is a class which is created inside another class with keyword inner. In other words, we can say that a nested class which is marked as “inner” is called inner class.
Inner class cannot be declared inside interfaces or non-inner nested classes.
The advantage of inner class over nested class is that, it is able to access members of outer class even it is private. Inner class keeps a reference to an object of outer class.
Kotlin Inner Class Example
Output:
code inside inner class name is Ashu Id is 101
Next TopicKotlin Constructor