Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
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.
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.
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.
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.
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.
Defining nested classes in Kotlin:
class OuterClass { class NestedClass { // Nested class definition } }
Accessing outer class members from a nested class in Kotlin:
class OuterClass { class NestedClass { // Cannot access members of OuterClass directly } }
Visibility modifiers in Kotlin nested classes:
private
or internal
.class OuterClass { private class NestedClass { // private nested class } }
Creating instances of nested classes in Kotlin:
val nestedInstance = OuterClass.NestedClass()
Declaring inner classes in Kotlin:
class OuterClass { inner class InnerClass { // Inner class definition } }
Accessing outer class members from an inner class in Kotlin:
class OuterClass { inner class InnerClass { fun accessOuterMember() { // Access members of OuterClass here } } }
Visibility modifiers in Kotlin inner classes:
class OuterClass { private inner class InnerClass { // private inner class with access to outer class's members } }
Creating instances of inner classes in Kotlin:
val outerInstance = OuterClass() val innerInstance = outerInstance.InnerClass()