How to Work With Nested Class in Kotlin?
Last Updated :
30 Jan, 2022
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") }
Similar Reads
How to Get the Class in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
How to Work with Inline Properties in Kotlin? In this article, we are going to discuss how to work with inline properties in Kotlin. but before that, you should know some basics of functions and should be familiar with OOPs concepts. A great thing about Kotlin is high-order functions that let us use functions as parameters to other functions. H
3 min read
Kotlin Nested class and Inner class In Kotlin, you can define a class inside another class. Such classes are categorized as either nested classes or inner classes, each with different behavior and access rules.Nested ClassA nested class is a class declared inside another class without the inner keyword. By default, a nested class does
3 min read
Singleton Class in Kotlin Singleton Class in Kotlin is also called as the Singleton Object in Kotlin. Singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere. Many times we create the two different objects of the same class, but we have to remember that
4 min read
How to Iterate Over a Class's Properties in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
How to Type Check an Object in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Working with Anonymous Function in Kotlin In Kotlin, we can have functions as expressions by creating lambdas. Lambdas are function literals that is, they are not declared as they are expressions and can be passed as parameters. However, we cannot declare return types in lambdas. Although the return type is inferred automatically by the Kot
3 min read
Restricting Class Hierarchies in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Merge Two Collections in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
4 min read
Working with Test Driven Development(TDD) in Android with Kotlin TDD is a software methodology, in which developers write tests for pieces of code prior to writing the code itself. If you are unfamiliar with this buzzword, please read this article first. In TDD, the test has to be written, followed by writing the code. The test may fail, the code will be updated,
5 min read