# Delegation to Java interface. Why does this fail?

**URL:** <https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835>\
**Category:** Uncategorized\
**Created:** [April 30, 2013, 11:20pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835 "2013-04-30T23:20:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkwatson](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jkwatson/32/1587_2.png) [@jkwatson](https://discuss.kotlinlang.org/u/jkwatson)\
**Post date:** [April 30, 2013, 11:20pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/1 "2013-04-30T23:20:42Z")

</div>

Can someone explain to me why this code compiles, but fails at runtime?

class Thing(delegate: CharSequence) : CharSequence by delegate

fun main(args: Array\<String\>) {

&nbsp;&nbsp;println(Thing("hello there").length())

}

It fails with this exception at runtime:

 

Exception in thread "main" java.lang.AbstractMethodError: Thing.length()I at kotlin.KotlinPackage$src$StringsJVM$230392736.length(StringsJVM.kt:146) at kotlin.KotlinPackage.length(Unknown Source) at \_DefaultPackage$src$CompileFail$-1787021120.main(CompileFail.kt:4) at \_DefaultPackage.main(CompileFail.kt:1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

I looked for an existing bug, but couldn’t find one that matched this.

Thanks!

John

---

<div class="post-metadata">

**Author:** ![dodyg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dodyg/32/1451_2.png) [@dodyg](https://discuss.kotlinlang.org/u/dodyg)\
**Post date:** [May 1, 2013, 11:57am UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/2 "2013-05-01T11:57:53Z")

</div>

This looks like a bug to me.

toString() works but all the other 3 methods fails (charAt(), subSequence() and length())

``

```kotlin
class Thing(delegate: CharSequence) : CharSequence by delegate
fun main(args: Array<String>) {
  println(Thing(“hello there”).toString())
}
```

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [May 2, 2013, 11:10pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/3 "2013-05-02T23:10:35Z")

</div>

Feel free to report issue to our tracker. Thank you.

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [May 2, 2013, 11:17pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/4 "2013-05-02T23:17:44Z")

</div>

Actually CharSequence in your code is not java.lang.CharSequence. It is jet.CharSequence.

``

```kotlin
public trait CharSequence {
  public fun get(index : Int) : Char
  public val length : Int
  public fun toString() : String
}
```

And your code should be look like

``

```kotlin

class Thing(delegate: CharSequence) : CharSequence by delegate

fun main(args: Array<String>) {
  println(Thing(“hello there”).length)
}

```

But it does not work too.

---

<div class="post-metadata">

**Author:** ![jkwatson](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jkwatson/32/1587_2.png) [@jkwatson](https://discuss.kotlinlang.org/u/jkwatson)\
**Post date:** [May 3, 2013, 12:28am UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/5 "2013-05-03T00:28:41Z")

</div>

Yeah, I think I tried that variation as well, and noticed it didn't work.&nbsp;&nbsp;My example should fail to compile, correct?

I’ll log the bug.

Thanks,  
&nbsp;&nbsp;John

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [May 3, 2013, 1:11pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/6 "2013-05-03T13:11:44Z")

</div>

Yes, I think, your example should not compile.

Thanks for your feedback.

---

<div class="post-metadata">

**Author:** ![jkwatson](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jkwatson/32/1587_2.png) [@jkwatson](https://discuss.kotlinlang.org/u/jkwatson)\
**Post date:** [May 3, 2013, 5:48pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/7 "2013-05-03T17:48:06Z")

</div>

I created 2 separate bugs for this, since I don't know if they are related or not.

Thanks!

John

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [May 3, 2013, 7:34pm UTC](https://discuss.kotlinlang.org/t/delegation-to-java-interface-why-does-this-fail/835/8 "2013-05-03T19:34:47Z")

</div>

It's ok, thanks.
