0
0
Kotlinprogramming~10 mins

Multiple interface implementation 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 declare a class that implements two interfaces.

Kotlin
interface A {
    fun foo()
}

interface B {
    fun bar()
}

class MyClass : [1] {
    override fun foo() {
        println("foo")
    }
    override fun bar() {
        println("bar")
    }
}
Drag options to blanks, or click blank then click option'
AA & B
BA, B
CA | B
DA B
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' or '|' instead of ',' to separate interfaces
Not separating interfaces at all
2fill in blank
medium

Complete the code to override the method from interface A.

Kotlin
interface A {
    fun greet(): String
}

class Person : A {
    override fun [1](): String {
        return "Hello"
    }
}
Drag options to blanks, or click blank then click option'
Ahello
Bgreeting
CsayHello
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than declared in the interface
Forgetting to override the method
3fill in blank
hard

Fix the error in the class declaration to implement both interfaces A and B.

Kotlin
interface A {
    fun foo()
}

interface B {
    fun bar()
}

class Sample : [1] {
    override fun foo() {}
    override fun bar() {}
}
Drag options to blanks, or click blank then click option'
AA, B
BA | B
CA & B
DA B
Attempts:
3 left
💡 Hint
Common Mistakes
Using spaces or symbols like '&' or '|' instead of commas
Not separating interfaces
4fill in blank
hard

Fill both blanks to complete the class declaration and method override.

Kotlin
interface X {
    fun action()
}

interface Y {
    fun action()
}

class Combined : [1] {
    override fun [2]() {
        println("Action performed")
    }
}
Drag options to blanks, or click blank then click option'
AX, Y
BX & Y
Caction
Dperform
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of ',' to separate interfaces
Using wrong method name in override
5fill in blank
hard

Fill all three blanks to implement two interfaces and override their methods correctly.

Kotlin
interface First {
    fun firstMethod()
}

interface Second {
    fun secondMethod()
}

class MultiImpl : [1] {
    override fun [2]() {
        println("First method")
    }
    override fun [3]() {
        println("Second method")
    }
}
Drag options to blanks, or click blank then click option'
AFirst, Second
BfirstMethod
CsecondMethod
DFirst & Second
Attempts:
3 left
💡 Hint
Common Mistakes
Using '&' instead of ',' to separate interfaces
Incorrect method names in overrides