# Delegates: No more need for Inheritance?

**URL:** <https://discuss.kotlinlang.org/t/delegates-no-more-need-for-inheritance/914>\
**Category:** Uncategorized\
**Created:** [February 11, 2013, 11:04pm UTC](https://discuss.kotlinlang.org/t/delegates-no-more-need-for-inheritance/914 "2013-02-11T23:04:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverPlow](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@OliverPlow](https://discuss.kotlinlang.org/u/OliverPlow)\
**Post date:** [February 11, 2013, 11:04pm UTC](https://discuss.kotlinlang.org/t/delegates-no-more-need-for-inheritance/914/1 "2013-02-11T23:04:54Z")

</div>

I lately had a look at Google Go which does not support inheritance. Instead it provides language built-in delegates. When I first saw it appeared absolutely obvious to me: no more single inheritance, multiple inheritance (with it's problems), traits (and stateful traits have their problems), nor mixins. It's all done through delegates. Okay, admittedly, delegates in Go are a simple construct. For instance, there is no protection of methods nor inst vars, and no late binding (which is a problem, I think). But in Kotlin these things can be done:

> trait Base { &nbsp;&nbsp;protected fun base() : Unit { &nbsp;&nbsp;println("base") &nbsp;&nbsp;} }
> 
> open class BaseImpl : Base {
> 
> }
> 
> trait Base2 {  
> &nbsp;&nbsp;fun base2() : Unit {  
> &nbsp;&nbsp;println(“base2”)  
> &nbsp;&nbsp;}  
> }
> 
> open class BaseImpl2 : Base2 {
> 
> }
> 
> open class Bar() {  
> &nbsp;&nbsp;protected fun bar() {  
> &nbsp;&nbsp;println(“bar”)  
> &nbsp;&nbsp;}  
> }
> 
> class Foo(b : Base = BaseImpl(), b2 : Base2 = BaseImpl2()) : Base by b, Base2 by b2, Bar() {  
> &nbsp;&nbsp;fun foo() : Unit {  
> &nbsp;&nbsp;base()  
> &nbsp;&nbsp;base2()  
> &nbsp;&nbsp;bar()  
> &nbsp;&nbsp;}  
> }

And then running this:

```kotlin
{
    val foo = Foo()
    foo.foo()

```

will print:

```kotlin
        base2
        bar

```

I don't really see a difference to inheritance here. You get just the same bang. You don't even see that you get it through delegates instead of inheritance. The solution above is a bit verbose, yes. You have to implement and additional class BaseImpl and BaseImpl2. Then, this is a little verbose compared to inheritance:

```
class Foo(b : Base = BaseImpl(), b2 : Base2 = BaseImpl2()) : Base by b, Base2 by b2
```

Nevertheless, I keep asking myself seriously whether we can cross inheritance out. Find a more concise way to define lists of delegates (see code above), get a fix for [KT-3029](http://youtrack.jetbrains.com/issue/KT-3029) and that's it. Don't get me wrong. I have been developing 10 years with Smalltalk (truly OO) and 10 years with Java (well, oops ...). But I think I know what inheritance is and I'm starting to get a feeling that it is a construct which back then was defined too narrow and resulted in a too narrow harness. Then it caught on, probably because people jump on everything that is new. And then it was a success you can't stop. I think late binding and classes were much more important than inheritance.

Any opinions?  
Cheers, Oliver

---

<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:** [February 12, 2013, 10:20am UTC](https://discuss.kotlinlang.org/t/delegates-no-more-need-for-inheritance/914/2 "2013-02-12T10:20:52Z")

</div>

1) We have to support inheritance to interop woth the rest of the world 2) Many problems of multiple inheritance arise with multiple delegation (see the early Kotlin inheritance model)

---

<div class="post-metadata">

**Author:** ![OliverPlow](https://avatars.discourse-cdn.com/v4/letter/o/d07c76/32.png) [@OliverPlow](https://discuss.kotlinlang.org/u/OliverPlow)\
**Post date:** [February 12, 2013, 11:41am UTC](https://discuss.kotlinlang.org/t/delegates-no-more-need-for-inheritance/914/3 "2013-02-12T11:41:20Z")

</div>

> 2) Many problems of multiple inheritance arise with multiple delegation (see the early Kotlin inheritance model)

Ah, I see. When I played with the code mentioned in the first post I renamed Base2.base2() to Base2.base() to see what happens. The ambiguity was flaged immediatedly by the IDE and hence I thought implicitly that diamond problems seem to be caught by the compiler. What a pity ... Might be the reason delegates in Go are that simple.

– Oliver
