Kotlin Nested Class

In this tutorial you will learn about the Kotlin Nested Class and its application with practical example.

Kotlin Nested Class

Nested Class: Nested class is a class which is declared inside the class or interface. In Kotlin, nested class is by default static, hence it can be accessed without creating any object of that class.

Syntax:

Example: You have to use . notataion for using nested class and its member with its enclosing class. Here Outer Class is an enclosing class of Nested Class. Let’s see below example.

Output: So when you simply run this code this will give output like below image.

nested-1

Kotlin Inner Class: Through kotlin nested class we can not access to outer class instance, to solve this issue we need to change nested class to inner class, Nested class has the capabilties to access outer class members.

Example: In below example we have change nested class to inner class.

Output: The output of this class will look like below image.

nested-2

In this tutorial we have learn about the Kotlin Nested Class and its application with practical example. I hope you will like this tutorial.