Kotlin Nested Class

We can create classes inside classes with Kotlin. Let’s create an example.

As you can see, first class is Box class and it has three properties; width, length and height. We can also define this class as “outer class”.

Second class is an inner class which called Content. It has a property and two functions.

You can notice that we can access Box class’ properties from inner class Content. We can do it because it is an inner class.

💁🏻‍♀ ️So, how can we use this classes and functions from main function?

We can’t access Content using outer class name directly. We should create an instance of the outer class and thus we can access inner class using this instance.

🦕 Sources

☄️ 🌙 You can access source code from my GitHub! 🧙🏼‍♀️

--

--