Challenge - 5 Problems
Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of interface default method call
What is the output of this Kotlin code when calling
greet() on an instance of Person?Kotlin
interface Greeter { fun greet() { println("Hello from Greeter") } } class Person : Greeter fun main() { val p = Person() p.greet() }
Attempts:
2 left
💡 Hint
Interfaces in Kotlin can have default method implementations.
✗ Incorrect
Since
Greeter interface provides a default implementation of greet(), the Person class inherits it without needing to override. So calling greet() prints the default message.❓ Predict Output
intermediate2:00remaining
Output when overriding default method
What will be printed when
greet() is called on Student instance?Kotlin
interface Greeter { fun greet() { println("Hello from Greeter") } } class Student : Greeter { override fun greet() { println("Hello from Student") } } fun main() { val s = Student() s.greet() }
Attempts:
2 left
💡 Hint
Overriding a default method replaces the original implementation.
✗ Incorrect
The
Student class overrides the default greet() method, so calling it prints the overridden message.🔧 Debug
advanced2:30remaining
Identify the error in interface default method usage
What error occurs when compiling this Kotlin code?
Kotlin
interface A { fun show() { println("A show") } } interface B { fun show() { println("B show") } } class C : A, B { override fun show() { super.show() } } fun main() { val c = C() c.show() }
Attempts:
2 left
💡 Hint
When multiple interfaces have the same default method, you must specify which one to call.
✗ Incorrect
Class C inherits two interfaces with the same default method. Calling super.show() is ambiguous and causes a compilation error. You must specify which interface's method to call using syntax like
super.show() or super.show().📝 Syntax
advanced2:00remaining
Correct syntax to call specific interface default method
Which option correctly calls the
show() method from interface B inside class C?Kotlin
interface A { fun show() { println("A show") } } interface B { fun show() { println("B show") } } class C : A, B { override fun show() { // call B's show here } }
Attempts:
2 left
💡 Hint
Use the syntax super.methodName() to call a specific interface's default method.
✗ Incorrect
To resolve ambiguity, Kotlin requires specifying the interface name in super call like
super.show().🚀 Application
expert3:00remaining
Determine output with multiple interface default methods and override
What is the output of this Kotlin program?
Kotlin
interface X { fun action() { println("X action") } } interface Y { fun action() { println("Y action") } } class Z : X, Y { override fun action() { super<X>.action() super<Y>.action() println("Z action") } } fun main() { val z = Z() z.action() }
Attempts:
2 left
💡 Hint
You can call multiple interface default methods explicitly using super.method()
✗ Incorrect
Class Z overrides action() and calls both interface default implementations explicitly, then prints its own message. So all three lines print in order.