Every developer knows the pain of repeatedly writing the same method that simply forwards calls to another object. This will make your class confusing, harder to read, and open the door to bugs. This pattern, commonly known as Decorator pattern or Proxy pattern, is essential but very tedious.
Kotlin, This language is designed to enhance the happiness of developers and has an elegant built-in solution that allows you to eliminate these template codes with just one keyword - by -. This is Kotlin's secret weapon: Class Delegation.
Let's delve deeper into how to clean up our code and embrace functional elegance.
What is class delegation?
Class delegation is a language feature that allows one class (delegate) to handle subsets of interface methods for another class (delegate) that implements the interface.
In simpler terms, you can implement an interface by saying, "Hey, I'm implementing this interface, but I hope this other object can handle all of its methods for me. Kotlin automatically generates all necessary forwarding methods - saving you the hassle of writing them yourself!
💡 Core difference: Eliminating transitive template code
To clearly illustrate the reduction of template code, let's use an extended LogWriter interface. Our goal is to create a wrapper that adds timestamps to standard messages, but for other critical logging operations, it will simply forward them.
The interface we need
![]()
Specific implementation (delegated party)
![]()
To manually create our TimestampedLogger, we must write override code for all three methods, even those simple pass through methods:
![]()
Using the 'by' keyword, we instruct Kotlin to handle the forwarding of the entire interface for us. We only need to write methods that add custom logic (i.e. logMessage).
![]()
The reduced template code is the insignificant forwarding code of methods that you intend to pass through unchanged, such as logError and flushLogs. This allows your decorator category to focus only on the new value it provides.
Advanced example: Delegate a configuration cache
This technology is particularly outstanding when building packers for improving performance or security. Let's take a look at how to manage application settings when the underlying storage speed is slow.
![]()
Can I delegate multiple interfaces in one class?
Yes, absolutely possible! Kotlin supports delegating multiple interfaces. You just need to use the 'by' keyword for each interface. For example: class MyMultiTasker: Interface A by aInstance, Interface B by bInstance {...}
What would happen if I override a delegated method?
When you rewrite a method, your rewritten implementation will be executed first. The delegation mechanism is only used for methods that you have not explicitly implemented. This is precisely why the Decorator mode works so simply!
Is this the same as attribute delegation (by lazy, by observable)?
No, they are different, but conceptually related.
Class delegation (e.g. class MyClass: Interface by delegate): handles interface implementation and method forwarding.
Attribute delegation (e.g. val x by lazy {...}): Implementation of handling attribute getter/setter and custom logic when accessing or modifying attributes.
Both use the same elegant 'by' keyword to achieve focus separation. To gain a deeper understanding of the differences between them and see practical examples of these two types of delegation, please refer to our follow-up article: Delegation Unveiling: Class Delegation and Attribute Delegation in Kotlin.








