0
0
Kotlinprogramming~5 mins

Inner class access to outer members in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an inner class in Kotlin?
An inner class in Kotlin is a class defined inside another class that can access the members of its outer class directly.
Click to reveal answer
intermediate
How does an inner class access the outer class's members?
An inner class can access the outer class's properties and functions directly using the <code>this@OuterClassName</code> syntax if needed.
Click to reveal answer
beginner
What keyword is used to declare an inner class in Kotlin?
The <code>inner</code> keyword is used before the class declaration to make it an inner class that holds a reference to the outer class.
Click to reveal answer
intermediate
What happens if you declare a nested class without the <code>inner</code> keyword?
A nested class without <code>inner</code> is static and cannot access the outer class's members directly.
Click to reveal answer
intermediate
Show how to access an outer class property named <code>name</code> from an inner class in Kotlin.
Inside the inner class, use this@OuterClass.name to access the outer class's name property.
Click to reveal answer
Which keyword makes a Kotlin nested class an inner class with access to outer members?
Ainner
Bouter
Cnested
Dstatic
How do you refer to the outer class instance from inside an inner class?
Athis.outer
Bthis@OuterClassName
Csuper
Douter.this
What is true about a nested class without the inner keyword?
AIt cannot access outer class members directly
BIt can access outer class members directly
CIt is the same as an inner class
DIt automatically inherits outer class
Which of these is a valid way to declare an inner class in Kotlin?
Astatic inner class InnerClass {}
Bclass inner InnerClass {}
Cclass InnerClass inner {}
Dinner class InnerClass {}
Why would you use an inner class instead of a nested class?
ATo make the class static
BTo prevent access to outer class
CTo access outer class members easily
DTo avoid creating an instance of outer class
Explain how an inner class in Kotlin can access the members of its outer class. Include syntax examples.
Think about how the inner class keeps a reference to the outer class instance.
You got /3 concepts.
    Describe the difference between a nested class and an inner class in Kotlin regarding access to outer class members.
    Focus on the presence or absence of the inner keyword.
    You got /3 concepts.