Signup/Sign In
LAST UPDATED: AUGUST 10, 2020

Kotlin Nested and Inner Class

In this tutorial, we will discuss the two ways to create a class inside another class in Kotlin, the first is Kotlin Nested class, and the other is Kotlin Inner class.

Kotlin Nested Class

We can create a Nested Class inside another class directly. By default, nested classes are static in nature. It means we can use the properties and functions of the nested class without creating an object of the outer class. They can be directly accessed using . operator.

In nested class:

  • Inside a nested class, we cannot access members of the outer class (Animal).

  • Nested class members are directly accessed using . operator without creating an object of the outer class.

Here is the syntax for Nested class:

class Outer{
    // Members of Outer class
    class Nested{
        // Members of Nested class
    }
}

and the syntax for creating an object of the Nested class:

val obj = Outer.Nested()

We can create an object of the nested class using the outer class name.

Kotlin Nested Class Example:

Let's take an example,

class Animal{
    // outer class property
    val legs = 4
    class Dog{
        // nested class proprty
        val say = "Woof"
        val hasTail = true
        fun tail(){
            println("Dog has tail: $hasTail")
        }
    }
}

fun main() {
    // Creating object of nested class directly
    val dog = Animal.Dog()
    // Accessing inner class
    dog.tail()
    println("Dog says: ${dog.say}")
}


Dog has tail: true
Dog says: Woof

Kotlin Inner Class

Kotlin Inner class is created using the inner keyword. It can be considered as a special nested class.

In an inner class:

  • The inner class maintains an instance of the outer class So, it can access members of the outer class.

  • We cannot declare an inner class inside an interface or non-inner class.

  • We cannot directly create an object of the inner class. It can be created using the outer class object.

Here is the syntax:

class Outer{
    // Members of Outer class
    inner class Inner{
        // Members of Inner class
    }
}

and to create the object of the inner class,

val outerObj = Outer()
// using outer class object to create inner class object
val innerObj = outerObj.Inner()

As you can see in the syntax above, we first create the object of the Outer class and then use it to create the object of the inner class.

Kotlin Inner Class Example:

Let's take an example,

class Animal{
    // outer class property
    val legs = 4
    inner class Dog{
        // inner class property
        val say = "Woof"
        val hasTail = true
        fun tail(){
            println("Dog has tail: $hasTail")
        }
        fun printLegs(){
            println("No of legs are: $legs")
        }
    }
}

fun main() {
    // Creating object of outer class
    val animal = Animal()
    // Creating object of inner class
    val dog = animal.Dog()
    // Accessing inner class
    dog.tail()
    dog.printLegs()
    println("Dog says: ${dog.say}")

}


Dog has tail: true
No of legs are: 4
Dog says: Woof

Summary

In this tutorial, we discussed Kotlin Nested class and Inner class. We also covered how to access their members from main() function. In the next tutorial, we will cover Data class in Kotlin.



About the author:
I'm a writer at studytonight.com.