Michal Ankiersztajn
1 min readJun 2, 2024

--

Hi, sorry for the late reply. No, if `CatMouth` implements `Bite` and we use delegate (SpecificCat: Mouth by...) SpecificCat won't acquire the methods. Unfortunately, Medium doesn't allow code snippets in comments, but I hope this example helps:

interface Mouth { fun speak() }

interface Bite { fun bite() }

class CatMouth : Mouth, Bite { ... }

class SpecificCat : Mouth by CatMouth()

fun main() {

val specificCat = SpecificCat()

specificCat.speak()

specificCat.bite() // Compilation error

}

If you have a situation where you want to delegate both `Bite` and `Mouth` in SpecificCat through `CatMouth`, then there are 2 approaches: either make `Mouth` implement `Bite`:

interface Mouth : Bite { ... }

Now:

specificCat.bite() // works

Or go with a more flexible approach using dependency injection (which I recommend):

class SpecificCat(

mouth: Mouth,

bite: Bite,

) : Mouth by mouth, Bite by bite

fun main() {

val mouth = CatMouth()

val specificCat = SpecificCat(mouth = mouth, bite = mouth)

specificCat.speak()

specificCat.bite() // works

}

This way, you can pass the same object instance to implement both interfaces.

--

--

Michal Ankiersztajn

Android/Kotlin Developer & Applied Computer Science Engineer