Nested class or interface

3 0 0
                                        

Nested class or interface - You are allowed to put a class or an interface within a class.

==================

fun main(){

    val obj1 = OuterClass()

    obj1.displaySomething()

    val obj2 = OuterClass.TheBook()

    println(obj2.describe())

}

class OuterClass{

    private val prop1 = "The Beginning"

    interface Book{

        fun describe(): String

    }

    class TheBook: Book{

         override fun describe(): String = "This is the book that you like."

     }

     fun displaySomething(){

          print("Book: $prop1 --- ")

     }

}

==================

Result:

Book: The Beginning --- This is the book that you like.


Note:

1. Regular class within another class cannot access the members of its outer class.

2. Creating an object of the outer class will not let you access the regular class within in. To access the regular class, you have to access it like a companion object. The outer class name followed by the dot notation and the regular class name.

3. Using regular class within another class is a hassle since you have to create two objects just to access the members of the class within and the members of the outer class.


inner - This is the keyword use for a regular class within an outer class to have a special privilege of accessing its members. It is like you have an implicit this keyword when accessing its members. Also, during invocation, you can just use the outer class object to access the inner class itself. So, one object is enough to access it all.

======================

fun main(){

    val obj1 = OuterClass()

    obj1.TheBook().displaySomething()

    println(obj1.TheBook().describe())

}

class OuterClass{

    private val prop1 = "The Beginning"

    interface Book{

        fun describe(): String

    }

    class TheBook: Book{

        override fun describe(): String = "This is the book that you like."

        fun displaySomething(){

            print("Book: $prop1 --- ")

        }

    }

}

======================

Result:

Book: The Beginning --- This is the book that you like.

Note: I did transfer the displaySomething() method inside the inner class since I am already allowed to access the outer class property. If there are functions in the outer class that you want to use, you are allowed to access it too.


Anonymous class - this is a class without a name.

==================


fun main(){

    val obj1 = OuterClass()

    obj1.TheBook().displaySomething()

    println(obj1.TheBook().describe())

    println(OuterClass.describe())

    println(OuterClass.TheBook2.describe())

    val x = object : OuterClass.Book{

        override fun describe(): String = "I am an anonymous class."

    }

    println(x.describe())

}

class OuterClass{

    private val prop1 = "The Beginning"

    interface Book{

        fun describe(): String

    }

    class TheBook: Book{

        override fun describe(): String = "This is the book that you like."

        fun displaySomething(){

            print("Book: $prop1 --- ")

        }

    }

    companion object: Book{

        override fun describe(): String = "I made this Book my companion."

    }

    object TheBook2: Book{

        override fun describe(): String = "This is an object class. Not a companion though."

    }

}

==================

Result:

Book: The Beginning --- This is the book that you like.

I made this Book my companion.

This is an object class. Not a companion though.

I am an anonymous class.


Kotlin ProgrammingWhere stories live. Discover now