BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Kotlin Nested and Inner Class with examples

Last Updated: March 13, 2019 by Chaitanya Singh | Filed Under: Kotlin Tutorial

In this guide, you will learn about nested and inner class in Kotlin with the help of examples.

Kotlin Nested Class

When a class is declared inside another class then it is called nested class. Nested classes are similar to static nested class in Java.

class OuterClass {
    ....
    class NestedClass {
        ...
    }
}

Points to Note:
1. A Nested class cannot access the members of the outer class.
2. To access the members of nested class, we use the dot (.) operator with outer class.

Kotlin Nested Class Example

In the following example we have an outer class and a nested class. This example demonstrate how to access the members of nested class using dot (.) operator.

class OuterClass {

    val oStr = "Outer Class"

    class NestedClass {
        val nStr = "Nested Class"
        fun demo() = "demo() function of nested class"
    }
}

fun main(args: Array<String>) {
    // accessing data member of nested class
    println(OuterClass.NestedClass().nStr)

    // accessing member function of nested class
    println(OuterClass.NestedClass().demo())

    // creating object of the Nested class
    val obj = OuterClass.NestedClass()
    println(obj.demo())
}

Output:
Kotlin Nested and Inner Class

Kotlin Nested class – cannot access members of outer class

The following program will throw compilation error because a nested class does not have access to the members of outer class. Here the nested class is trying to access the member oStr which belongs to outer class, thus we are getting a compilation error. We can solve this issue by using inner class which is discussed in next section of this same article.

class OuterClass {

    val oStr = "Outer Class"

    class NestedClass {
        val nStr = "Nested Class"
        fun demo() = "demo() function using $oStr"
    }
}

fun main(args: Array<String>) {
    println(OuterClass.NestedClass().demo())
}

Output:

Compilation Error

Kotlin Inner class

Kotlin inner class is declared using inner modifier. Inner classes have access to the members of the outer class. Lets take the same example that we have seen above using the inner class instead of nested class. Here we are accessing the member oStr of outer class using the inside inner class.

class OuterClass {

    val oStr = "Outer Class"

    inner class InnerClass {
        val nStr = "Nested Class"
        fun demo() = "demo() function using $oStr"
    }
}

fun main(args: Array<String>) {
    val o = OuterClass()
    println("${o.InnerClass().demo()}")

    val i = OuterClass().InnerClass()
    println("${i.demo()}")
}

Output:
Kotlin Inner Class

❮ PreviousNext ❯

Top Related Articles:

  1. Kotlin Interfaces with examples
  2. Kotlin Constructors with examples
  3. Kotlin Abstract Class with examples
  4. Kotlin Keywords, Soft Keywords and Identifiers
  5. Kotlin Try as an expression in Exception handling

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Kotlin Tutorial

  • Kotlin Tutorial
  • Kotlin in Eclipse IDE
  • Kotlin in IntelliJ
  • First Kotlin Program
  • Kotlin Keywords
  • Kotlin Variables
  • Kotlin Type Casting
  • Kotlin operators
  • Kotlin Input/Output
  • Kotlin Comments

Kotlin String

  • Kotlin String

Kotlin Array

  • Kotlin Array
  • Kotlin Range

Control Flow

  • Kotlin if expression
  • Kotlin when expression
  • Kotlin for loop
  • Kotlin while loop
  • Kotlin do-while loop
  • Kotlin continue
  • Kotlin break

Kotlin Function

  • Kotlin Function
  • Kotlin Recursion
  • Kotlin default and named arguments
  • Lambda functions
  • Higher Order Function

Exception Handling

  • Exception Handling
  • Kotlin try catch
  • Multiple catch blocks
  • Nested try catch
  • Throw keyword
  • Kotlin try expression

Kotlin OOP

  • Class and Objects
  • Kotlin Constructors
  • Kotlin Inheritance
  • Visibility Modifiers
  • Kotlin abstract class
  • Kotlin Interfaces
  • Nested and Inner Class
  • Kotlin Data Class
  • Kotlin Sealed Class

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap