Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to call the member function instead of the extension function.
Kotlin
class Example { fun greet() = "Member" } fun Example.greet() = "Extension" fun main() { val ex = Example() println(ex.[1]()) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the extension function by using a different name.
Assuming extension functions override member functions.
✗ Incorrect
Member functions have higher priority than extension functions with the same name, so calling 'greet()' calls the member function.
2fill in blank
mediumComplete the code to call the extension function explicitly.
Kotlin
class Example { fun greet() = "Member" } fun Example.greet() = "Extension" fun main() { val ex = Example() println([1](ex)) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the member function instead of the extension function.
Using incorrect syntax to call the extension function.
✗ Incorrect
To call the extension function explicitly when a member function with the same name exists, use the syntax 'Example.greet()' with the receiver object.
3fill in blank
hardFix the error in calling the extension function inside another extension function.
Kotlin
class Example { fun greet() = "Member" } fun Example.greet() = "Extension" fun Example.callGreet() = [1](this) fun main() { val ex = Example() println(ex.callGreet()) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'greet()' without qualification calls the member function.
Using 'this.greet()' calls the member function, not the extension.
✗ Incorrect
Inside an extension function, to call another extension function when a member function with the same name exists, qualify it with the class name.
4fill in blank
hardFill both blanks to define an extension function and call the member function inside it.
Kotlin
class Example { fun greet() = "Member" } fun Example.[1]() = "Extension calls " + this.[2]() fun main() { val ex = Example() println(ex.greet()) println(ex.[1]()) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the extension function the same as the member function.
Not calling the member function correctly inside the extension.
✗ Incorrect
The extension function is named 'greetExtension' and calls the member function 'greet()' using 'this.greet()'.
5fill in blank
hardFill all three blanks to define an extension function that calls another extension and the member function.
Kotlin
class Example { fun greet() = "Member" } fun Example.[1]() = "Extension1" fun Example.[2]() = "Extension2 calls " + [3].greet() fun main() { val ex = Example() println(ex.greet()) println(ex.[1]()) println(ex.[2]()) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both extension functions.
Using 'Example' instead of 'this' to call the member function.
✗ Incorrect
The first extension is 'greet1', the second is 'greet2' which calls the member function 'greet()' on 'this' receiver.