W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/20/2021

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.

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.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.