Industrial Training




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.


class outerClass{  
   //outer class code  
    class nestedClass{  
      //nested class code  
    }  
}  


Kotlin Nested Class Example


class outerClass{  
    private var name: String = "Ashu"  
    class nestedClass{  
var description: String = "code inside nested class"  
        private var id: Int = 101  
        fun foo(){  
          //  print("name is ${name}") // cannot access the outer class member  
println("Id is ${id}")  
        }  
    }  
}  
fun main(args: Array){  
// nested class must be initialize  
println(outerClass.nestedClass().description) // accessing property  
var obj = outerClass.nestedClass() // object creation  
    obj.foo() // access member function  
}  

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.


class outerClass{  
   //outer class code  
    inner class innerClass{  
      //nested class code  
    }  
}  


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


class outerClass{  
     private  var name: String = "Ashu"  
     inner class  innerClass{  
var description: String = "code inside inner class"  
        private var id: Int = 101  
       fun foo(){  
println("name is ${name}") // access the outer class member even private  
println("Id is ${id}")  
        }  
    }  
}  
fun main(args: Array){  
println(outerClass().innerClass().description) // accessing property  
var obj = outerClass().innerClass() // object creation  
    obj.foo() // access member function  
  
}  

Output:
code inside inner class
name is Ashu
Id is 101



Hi I am Pluto.