Monday 22 January 2018

Kotlin: Access super class of outer class from inner class

By using the super keyword qualified with outer class name, we can access the super class of the outer class. Let’s me explain with an example.

Ex

Find below working application.

HelloWorld.kt
open class GrandParentClass {
 fun grandParentFunction() {
  println("I am in grand parent class")
 }
}

open class ParentClass : GrandParentClass() {
 fun parentClass() {
  println("I am in parent class")
 }

 inner class ChildInnerClass {
  fun printInfo() {
   parentClass()
   super@ParentClass.grandParentFunction()
  }
 }
}

fun main(args: Array<String>) {
 var innerObj = ParentClass().ChildInnerClass()

 innerObj.printInfo()
}

Output
I am in parent class
I am in grand parent class



Previous                                                 Next                                                 Home

No comments:

Post a Comment