0
0
Kotlinprogramming~10 mins

Inner class access to outer members in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to access the outer class property from the inner class.

Kotlin
class Outer {
    val greeting = "Hello"
    inner class Inner {
        val greeting = "inner"
        fun greet() = [1]
    }
}
Drag options to blanks, or click blank then click option'
Athis@Outer.greeting
Bthis.greeting
COuter.greeting
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using just 'greeting' or 'this.greeting' accesses inner class scope, not outer.
2fill in blank
medium

Complete the code to call the outer class function from the inner class.

Kotlin
class Outer {
    fun outerFun() = "Outer Function"
    inner class Inner {
        fun outerFun() = "inner"
        fun callOuter() = [1]
    }
}
Drag options to blanks, or click blank then click option'
AouterFun()
Bthis.outerFun()
Cthis@Outer.outerFun()
DOuter.outerFun()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling outerFun() directly calls the inner class function.
3fill in blank
hard

Fix the error in accessing the outer property inside the inner class method.

Kotlin
class Outer {
    val number = 42
    inner class Inner {
        val number = 0
        fun printNumber() {
            println([1])
        }
    }
}
Drag options to blanks, or click blank then click option'
Anumber
Bthis@Outer.number
Cthis.number
DOuter.number
Attempts:
3 left
💡 Hint
Common Mistakes
Using number or this.number accesses the inner class property.
4fill in blank
hard

Fill both blanks to access outer class property and call its function from inner class.

Kotlin
class Outer {
    val text = "Hi"
    fun outerFun() = "Called"
    inner class Inner {
        val text = "inner"
        fun outerFun() = "inner func"
        fun combined() = [1] + " " + [2]()
    }
}
Drag options to blanks, or click blank then click option'
Athis@Outer.text
Btext
Cthis@Outer.outerFun
DouterFun
Attempts:
3 left
💡 Hint
Common Mistakes
Using unqualified names causes errors or accesses inner class members.
5fill in blank
hard

Fill all three blanks to create a map with outer property keys and inner class method results as values.

Kotlin
class Outer {
    val prefix = "Key"
    inner class Inner {
        fun suffix() = "Value"
        fun createMap() = mapOf([1] to [2].[3]())
    }
}
Drag options to blanks, or click blank then click option'
Aprefix
Bthis@Outer
Cthis
Dsuffix
Attempts:
3 left
💡 Hint
Common Mistakes
Using this@Outer to call inner methods causes errors.