Open In App

How to Work With Nested Class in Kotlin?

Improve
Improve
Like Article
Like
Save
Share
Report

A class is declared within another class then it is called a nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating an 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 the outer class, but we can access the property of nested class from the outer class without creating an object for nested class.

Work With Nested Class in Kotlin

Now we will see how to work with a nested class in the following steps: Let’s try an example of a nested class in Kotlin:

Kotlin




fun main(args: Array<String>){
    var al = outCl()
    al.printAB()
    outCl.inCl().printB()
}
class outcl{
    var a = 6 
    fun printAB(){
        var b_ = incl().b
        printin("a = $a and b = $b_from inside outCl")
    }
    class incl{
        var b = "9" 
        fun printB(){
            printin("b = $b from inside incl")
        }
    }
}


Output:

a = 6 and b = 9 from inside outCl
b = 9 from inside incl

Now, let’s try an example of the inner class. To declare a nested class as inner, we use the inner keyword. An inner class can access members of the outer class, as they carry a reference to the outer class:

Kotlin




fun main(args: Array<String>) {
  var a = outcl()
  a.printAB()
  a.incl().printAB()
}
class outcl {
  var a = 6
  fun printAB() {
    var b_ = incl().b
    println ("a = $a and b = $b_ from inside outCl")
  }
  inner class incl {
    var b = "9"
    fun printAB() {
      printin ("a = $a and b = $b from inside incl")
    }
  }
}


Output:

a = 6 and b = 9 from inside outcl
b = 6 and b = 9 from inside incl

A nested class can be created by just declaring the nested class inside another class. In this case, to access the nested class, you make a static reference that is like outerClass.innerClass(), and you can also make an object of inner class using this. An inner class, on the other hand, is created by adding the inner keyword to a nested class. In that case, we access the inner class as though it was a member of the outer class, that is, using an object of the outer class like this:

var outerClassObject = outerClass ()
outerClassObject.innerClass ().memberVar

A nested class does not have access to members of the outer class, as it does not have any reference to an object of the outer class. On the other hand, the inner class can access all of the outer class’s members, as it has a reference to an object of the outer class.

We can also create anonymous inner classes in Kotlin using the object keyword, like this:

Kotlin




val customText TemplateListener = object : ValueEventListener{
  override fun onCancelled (p0: DatabaseError?) {
  }
  override fun onDataChange (dataSnapshot: DataSnapshot?) {
  }
}


On the JVM, if the object is an instance of a functional Java interface (that means a Java interface with a single abstract method), you can create it using a lambda expression prefixed with the type of the interface:

val listener = ActionListener { println(“clicked”) }



Last Updated : 30 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads