What is Delegation Interface in Kotlin?
Tags: kotlin, beginners, android
Published: 2023-02-03 23:00:00

**Simple explanation of delegation interface in Kotlin with some examples**

You may be familiar with [delegated properties](https://vtsen.hashnode.dev/complete-c-to-kotlin-syntax-comparisons#heading-by-lazy-delegated-properties) in Kotlin, but have you heard of [delegation interface](https://kotlinlang.org/docs/delegation.html#overriding-a-member-of-an-interface-implemented-by-delegation)? This is one of the features other programming languages do NOT have.

## Standard Interface Implementation

Let's say you have 2 interfaces below - `Interface1` and `Interface2`.

```kotlin
interface Interface1 {
    fun fun1()
}

interface Interface2 {
    fun fun2()
}
```

You have a `Class` that implements these 2 interfaces. So it overrides the `fun1()` and `fun2()` from these 2 interfaces.

```kotlin
class Class : Interface1, Interface2{
    override fun fun1() {
        println("fun1")
    }

    override fun fun2() {
        println("fun2")
    }
}
```

In short, the class diagram looks like this.

![delegation_interface_1.drawio.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664537454149/5VNk3lW_Y.png)

The caller usage looks like this.

```kotlin
fun main() {
    val obj = Class()
    obj.fun1()
    obj.fun2()
}
```

There is nothing fancy here, it is a pretty standard interface implementation.

## Delegation Interface Implementation

Another way to implement this standard interface above is using the delegation interface. Let's look at the delegation interface implementation class diagram below.

![delegation_interface_2.drawio.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1664537967847/k_TFDoZ7d.png)

The `Class` has no longer implementing `fun1()` and `fun2()`. **It delegates the implementations to instances of** `Interface1Impl` and `Interface2Impl`.

The code looks like this now.

```kotlin
interface Interface1 {  
    fun fun1()  
}  
  
interface Interface2 {  
    fun fun2()  
}  
  
class Interface1Impl: Interface1 {  
    override fun fun1() {  
        println("fun1")  
    }  
}  
  
class Interface2Impl: Interface2 {  
    override fun fun2() {  
        println("fun2")  
    }  
}  
  
class Class : Interface1 by Interface1Imp(), Interface2 by Interface2Imp() {  
  
}
```

As you can see, `fun1()` and `fun2()` implementations in `Class` have been moved out to `Interface1Impl` and `Interface2Impl`. The benefit of doing this is making the `Class` less clutter, not bloated with different kind of interface implementations.

> Please note the `Interface1Imp()` and `Interface2Imp()` are the instances of the class.

You can also override the delegation interface. `Interface1Impl.fun1()` is overridden by `Class.fun1()` below.

```kotlin
class Class : Interface1 by Interface1Imp(), Interface2 by Interface2Imp() {  
    override fun fun1() {  
        println("override fun1")  
    }  
}
```

## Summary

Well, it seems like the **delegation interface is just a sugar syntax**. I haven't personally used it yet. I do not know whether I'm going to eventually use it. It looks like it could be a good option to replace inheritance.

For practical usage, maybe you can also watch this video from Phillip Lackner.

{% youtube https://www.youtube.com/watch?v=MfJB-JhRAoQ %}

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/what-is-delegation-interface-in-kotlin)*.