# Under the hood of Kotlin Class Delegation

DevFeed: [Under the hood of Kotlin Class Delegation](<https://devfeed.tech/articles/under-the-hood-of-kotlin-class-delegation-25974.md>)

Original publisher: [Read original article](<https://medium.com/snapp-mobile/under-the-hood-of-kotlin-class-delegation-24d53f9aa924?source=rss-8efc0359e234------2>)

Author: Jossi Wolf

Published: 2019-04-09T13:20:12Z

Content type: article

Language: en

Sources: [Jossi Wolf](<https://devfeed.tech/sources/stories-by-jossi-wolf-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [class](<https://devfeed.tech/topics/class.md>), [superclass](<https://devfeed.tech/topics/superclass.md>), [interface implementation](<https://devfeed.tech/topics/interface-implementation.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>), [adapter pattern](<https://devfeed.tech/topics/adapter-pattern.md>), [Image](<https://devfeed.tech/topics/image.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [bytecode](<https://devfeed.tech/tags/bytecode.md>), [class](<https://devfeed.tech/tags/class.md>), [composition-over-inheritance](<https://devfeed.tech/tags/composition-over-inheritance.md>), [delegation](<https://devfeed.tech/tags/delegation.md>), [hood](<https://devfeed.tech/tags/hood.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [interface](<https://devfeed.tech/tags/interface.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [logic](<https://devfeed.tech/tags/logic.md>), [stories](<https://devfeed.tech/tags/stories.md>)

## AI overview

Kotlin class delegation can expose an API through a class without filling that class with proxy calls. In the example, a custom view delegates an interface's methods to a separate implementation using Kotlin's `by` keyword, keeping the logic extracted and the view less cluttered. The excerpt explains that Kotlin generates functions that invoke the delegate; with many methods, this generated code adds to compiled size even as it reduces clutter in the source.

## Source excerpt

I was told to add an image to the post, so here's one Photo by Jf Brou from Unsplash. Bonus points since Samoyeds are some of the cutest dogs out there! A piece of rather popular advice heard in software development is "favour composition over inheritance". I won't go into the details of why as there is an excellent chapter in "Effective Java" about it, as well as this nice article by Eric Lin. This article explores how class delegation can be useful and how it works under the hood. In our case, we had a custom view which should expose an interface looking like this: https://medium.com/media/2efb0d0125ffec7f388ee2df190bcf6a/href Now it was time to create the CanvasCapabilities implementation. https://medium.com/media/3d8b72ac99b28b99f39c95c882a69dce/href Of course, we don't want to start putting logic into our view just for the sake of exposing the interface through it, which meant that the view now had to act as a "proxy" (implementing the interface and calling the ArtboardCanvas ): https://medium.com/media/f3d51d082e7f23d725ef7efafb3fe131/href Not a nice solution, but at least we extracted the logic. The view was still cluttered though :( Class Delegation to the rescue Using the by keyword in Kotlin, properties and even classes can be delegated. You might know it from using lazy : https://medium.com/media/cbea2e6d1363f31e28552d9a8c87823e/href You can also delegate inheritance, which is what we are after. The usage looks like this: https://medium.com/media/5482d423d1b2a0fcff239f25c4ab31e9/href This delegates the methods from CanvasCapabilities to ArtboardCanvas . No more cluttering with "proxy" calls! Under The Hood Under the hood, Kotlin actually doesn't do any "black magic": It just generates a function which invokes the delegate. The bytecode, decompiled to Java looks like this: https://medium.com/media/34cf5d85a4bcf122bc98d74425e9a5dd/href If you have a lot of methods, that code will still be generated and adding to your compiled code's size, but at least it do