DEV Community

Cover image for What is Delegation Interface in Kotlin?
Vincent Tsen
Vincent Tsen

Posted on • Originally published at vtsen.hashnode.dev

What is Delegation Interface in Kotlin?

Simple explanation of delegation interface in Kotlin with some examples

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()
}
Enter fullscreen mode Exit fullscreen mode

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")
    }
}
Enter fullscreen mode Exit fullscreen mode

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()
}
Enter fullscreen mode Exit fullscreen mode

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() {  

}
Enter fullscreen mode Exit fullscreen mode

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")  
    }  
}
Enter fullscreen mode Exit fullscreen mode

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.


Originally published at https://vtsen.hashnode.dev.

Latest comments (1)

Collapse
 
ravisaini1990 profile image
Ravi Kumar Saini

Thanks for explaining it so well