Interfaces Workbook
Practice problems for Kotlin Interfaces Carry More Than Java’s Ever Could. Each takes a minute or two. Write your own answer first, then click Show answer — nothing here is a trick question, just direct practice of the syntax from the lesson.
declaring and implementing
1. Implement an interface
Declare interface Greeter { fun greet(name: String): String } and a class Formal that implements it.
Show answer Hide answer
interface Greeter {
fun greet(name: String): String
}
class Formal : Greeter {
override fun greet(name: String) = "Good evening, $name."
}A class adopts an interface with : — there’s no implements keyword — and override is required.
2. A default method
Add a greetAll(names: List<String>) default method to Greeter that greets each name, built on greet.
Show answer Hide answer
interface Greeter {
fun greet(name: String): String
fun greetAll(names: List<String>) =
names.joinToString("\n") { greet(it) }
} 3. A property contract
Declare interface Named requiring a val name: String and providing a derived val label that returns "Name: <name>".
Show answer Hide answer
interface Named {
val name: String
val label: String
get() = "Name: $name"
} 4. Provide the property
Write class User(...) implementing Named by supplying name through its constructor.
Show answer Hide answer
class User(override val name: String) : Namedlabel is inherited; only name must be provided.
clashes and functional interfaces
5. Resolve a clash
interface A { fun ping() = "A" } and interface B { fun ping() = "B" }. Write class C : A, B whose ping() returns "AB".
Show answer Hide answer
class C : A, B {
override fun ping() = super<A>.ping() + super<B>.ping()
}When two defaults clash the compiler forces you to choose, using super<Interface>.
6. A functional interface
Declare a fun interface Validator { fun isValid(s: String): Boolean } and create a validator for non-blank strings using a lambda.
Show answer Hide answer
fun interface Validator {
fun isValid(s: String): Boolean
}
val notBlank = Validator { it.isNotBlank() } inheritance and choices
7. Interface extends interfaces
Given interface Named { val name: String } and interface Aged { val age: Int }, declare interface Person that combines both and adds a default describe().
Show answer Hide answer
interface Person : Named, Aged {
fun describe() = "$name, $age"
} 8. Implement two interfaces
Declare class Robot implementing both Named and Aged (each with one property).
Show answer Hide answer
class Robot(override val name: String, override val age: Int) : Named, AgedA class can implement any number of interfaces.
9. Shared stored state
Implementers need to share a stored cache field. Write the base type as the kind that can hold one, with an abstract load(key: String): String.
Show answer Hide answer
abstract class Repository {
protected val cache = mutableMapOf<String, String>()
abstract fun load(key: String): String
}An interface can declare a property but can’t hold a backing field, so shared stored state calls for an abstract class.
10. A default built on abstract members
Declare interface Counter with an abstract count(): Int and a default isEmpty() returning whether the count is zero.
Show answer Hide answer
interface Counter {
fun count(): Int
fun isEmpty() = count() == 0
} Back to the lesson, Kotlin Interfaces Carry More Than Java’s, or on to the next one: extension functions.