Vincent Tsen
Vincent Tsen - AndroidDev Blog

Follow

Vincent Tsen - AndroidDev Blog

Follow

What is Delegation Interface in Kotlin?

Simple explanation of delegation interface in Kotlin with some examples

Vincent Tsen's photo
Vincent Tsen
ยทJan 13, 2023ยท

2 min read

What is Delegation Interface in Kotlin?

Table of contents

  • Standard Interface Implementation
  • Delegation Interface Implementation
  • Summary

You may be familiar with delegated properties in Kotlin, but have you heard of delegation interface? 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.

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.

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

The caller usage looks like this.

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

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.

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.

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.

Did you find this article valuable?

Support Vincent Tsen by becoming a sponsor. Any amount is appreciated!

See recent sponsors |ย Learn more about Hashnode Sponsors
ย 
Share this