# Delegation and mixins

**URL:** <https://discuss.kotlinlang.org/t/delegation-and-mixins/510>\
**Category:** Uncategorized\
**Created:** [April 19, 2014, 10:52am UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510 "2014-04-19T10:52:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndroidCactus](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@AndroidCactus](https://discuss.kotlinlang.org/u/AndroidCactus)\
**Post date:** [April 19, 2014, 10:52am UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/1 "2014-04-19T10:52:56Z")

</div>

I am trying to find some elegant solution how to add functionality to existing class hierarchies.

I found this old discussion about very same problem   
[http://blog.jetbrains.com/kotlin/2011/08/multiple-inheritance-part-2-possible-directions/#comment-125](http://blog.jetbrains.com/kotlin/2011/08/multiple-inheritance-part-2-possible-directions/#comment-125)  
There’s suggested solution to use delegation but it looks to me that’s not possible because methods in Activity superclass overriden by delegation are never called and that’s showstopper for android.

```kotlin
open class Activity {
    open fun onCreate() = println("Activity create")
}

trait Callbacks {
  fun onCreate()
}
class CallbacksImpl: Callbacks  {
  override fun onCreate() = println(“Callbacks create”)
}
```

When I do this:

```kotlin
class MyActivity: Activity(), Callbacks by CallbacksImpl()
val activity = MyActivity()
activity.onCreate() // prints ONLY “Callbacks create”
```

I can see no way how to change this code (without significantly touching MyActivity) to make it print:

```kotlin
“Activity create”
“Callbacks create”
```

First I was thinking that “requiring” Activity in Callbacks would help but even this looks like dead end.

```kotlin
trait Callbacks: Activity {
  override fun onCreate() {
  // ?
  }
}
```

In Java is this problem “solved” by making many subclasess like CallbacksActivity, CallbacksPreferenceActivity, … which are exactly the same.  
I was hoping Kotlin can provide better solution but it seems that traits and delegation are usefull only for presenting new functionality but not extending existing one.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 19, 2014, 11:10am UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/2 "2014-04-19T11:10:34Z")

</div>

The problem is that when you require a class in a trait, you can't make a "super.onCreate()" call, because inheritance may turn out to be indirect. But you can do something like this:

```kotlin
open class Activity {
    open fun onCreate() = println("Activity create")
}

abstract class ExtensibleActivity: Activity() {
  override fun onCreate() {
  super.onCreate()
  doOnCreate()
  }
  
  abstract fun doOnCreate()
}
trait Callbacks : ExtensibleActivity {
  override fun doOnCreate() {
  println(“Callbacks create”)
  }
}
class MyActivity: ExtensibleActivity(), Callbacks
fun main(args : Array<String>) {
  val activity = MyActivity()
  activity.onCreate() // prints both “Activity create” and “Callbacks create”
}

 
```

Live version: [http://kotlin-demo.jetbrains.com/?publicLink=104074971561017308771-1411938433](http://kotlin-demo.jetbrains.com/?publicLink=104074971561017308771-1411938433)

---

<div class="post-metadata">

**Author:** ![AndroidCactus](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@AndroidCactus](https://discuss.kotlinlang.org/u/AndroidCactus)\
**Post date:** [April 19, 2014, 12:08pm UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/3 "2014-04-19T12:08:54Z")

</div>

My description probably wasn't clear enough. The problem is there are already multiple subclasses of Activity and I want Callbacks to be usable with all of them.

Something like this exists in framework and I can’t change it:

```kotlin
open class Activity {
  open fun onCreate() = println(“Activity create”)
}
open class ActivityA: Activity()
open class ActivityB: Activity()
```

But I would like to do something like this:

```kotlin
class MyActivityA: ActivityA(), Callbacks by CallbacksImpl()
class MyActivityB: ActivityB(), Callbacks by CallbacksImpl()
```

It would be nice to have only one implementation of Callbacks which can be mixed in to any activity. It works fine until I have to call super method, which is this case.  
I was trying to avoid creating helper subclass for every Activity type. That’s what I am doing in Java.

Taking your code, I have to create this:

```kotlin
abstract class ExtensibleActivityA: ActivityA() { … }
abstract class ExtensibleActivityB: ActivityB() { … }
```

and implementation of both will be identical, right?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [April 19, 2014, 4:13pm UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/4 "2014-04-19T16:13:56Z")

</div>

Yes, if you have many activity classes that you can't change, it's a problem.

---

<div class="post-metadata">

**Author:** ![AndroidCactus](https://avatars.discourse-cdn.com/v4/letter/a/eb8c5e/32.png) [@AndroidCactus](https://discuss.kotlinlang.org/u/AndroidCactus)\
**Post date:** [August 15, 2014, 2:52pm UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/5 "2014-08-15T14:52:25Z")

</div>

Why exacly we can't call "super.someMethod()" in a trait? Is it because concrete class defined as supertype of a trait isn't actualy it's supertype but it's more like a constraint saying which classes can implement this trait? So "super" keyword in a class would behave different than in a trait?

I expect there are some more serious reasons why you prohibited this. I think it would be “super” useful if we could do this.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [August 15, 2014, 6:07pm UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/6 "2014-08-15T18:07:57Z")

</div>

First, we don't know if there is any implementation of a particular method in the superclass: the actual superclass may easily override it to be abstract.

Also, there’s no sane way to do it technically: the corresponding bytecode instruction (invokespecial) is only allowed in the actual subclass, and the code for trait method bodies is emitted to an auxiliary static class.

---

<div class="post-metadata">

**Author:** ![lionheart73](https://avatars.discourse-cdn.com/v4/letter/l/46a35a/32.png) [@lionheart73](https://discuss.kotlinlang.org/u/lionheart73)\
**Post date:** [July 22, 2019, 10:46pm UTC](https://discuss.kotlinlang.org/t/delegation-and-mixins/510/7 "2019-07-22T22:46:43Z")

</div>

What if you supported as keywords on class delegate. I am thinking:

class X(name: String): Callbacks by CallbacksImpl(name) as callbacks {  
fun y() = callbacks.y() +1  
}

That would play well with delegation it would also help with conflict resolution because we could resolve ambiguity with an explicit function.

Just a thought obviously hard done than said.
