Complete the code to declare a companion object inside the class.
class MyClass { companion object [1] { fun greet() = "Hello" } }
In Kotlin, the keyword companion is lowercase when declaring a companion object.
Complete the code to make the companion object implement the interface.
interface Greeter {
fun greet(): String
}
class MyClass {
companion object : [1] {
override fun greet() = "Hi"
}
}The companion object implements the interface named Greeter.
Fix the error in the companion object declaration to correctly implement the interface.
interface Speaker {
fun speak(): String
}
class Talk {
companion object [1] Speaker {
override fun speak() = "Hello"
}
}In Kotlin, to implement an interface, use a colon : before the interface name.
Fill both blanks to declare a companion object that implements two interfaces.
interface A {
fun a()
}
interface B {
fun b()
}
class Multi {
companion object [1] [2] {
override fun a() {}
override fun b() {}
}
}Use a colon : to start the interface list and commas , to separate multiple interfaces.
Fill both blanks to create a companion object that implements an interface and has a function.
interface Logger {
fun log(message: String)
}
class App {
companion object : Logger {
fun info(msg: String) = println(msg)
override fun log(message: String) = info([1])
}
}
fun main() {
App.Companion.log([2])
}Use colon : to implement the interface, pass the parameter message to info, and call log with the string "App started".