# SAM/Functional Interfaces and interoperability with Java

**URL:** <https://discuss.kotlinlang.org/t/sam-functional-interfaces-and-interoperability-with-java/25118>\
**Category:** Support\
**Created:** [June 29, 2022, 2:14pm UTC](https://discuss.kotlinlang.org/t/sam-functional-interfaces-and-interoperability-with-java/25118 "2022-06-29T14:14:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kasisoft](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kasisoft/32/9397_2.png) [@kasisoft](https://discuss.kotlinlang.org/u/kasisoft)\
**Post date:** [June 29, 2022, 2:14pm UTC](https://discuss.kotlinlang.org/t/sam-functional-interfaces-and-interoperability-with-java/25118/1 "2022-06-29T14:14:54Z")

</div>

I’m porting a library of mine to Kotlin though I would still keep it usable in Java (btw. I’m a Kotlin novice).  
So here’s an Java example where I’m stuck (I could rewrite some code but I obviously would prefer a nice solution):

```auto
public class MPredicate<T> {

  boolean test(to: T) throws Exception;

  default MPredicate<T> negate() {
      return $ -> !test($);
  }

}

```

Obviously this contains two functions though one is a default one. In Java I can simply use this functional interface like this:

```auto
public class Dodo {
  private static MPredicate<String> PREDICATE = $ -> "dodo".equals($);
}

```

However if I port **MPredicate** to Kotlin this won’t fly as the lambda expression doesn’t allow to be instantiated due to the additional **negate** method (SAM conversion doc explicitly states that only one function is allowed).

Is there a way to solve this nicely (extracting those additional methods is possible but I’d like to keep these default methods around)?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [June 29, 2022, 7:30pm UTC](https://discuss.kotlinlang.org/t/sam-functional-interfaces-and-interoperability-with-java/25118/2 "2022-06-29T19:30:29Z")

</div>

> [@kasisoft](#):
>
> SAM conversion doc explicitly states that only one function is allowed

Where did you read this? Docs say something quite opposite:

> The functional interface can have several non-abstract members but only one abstract member.

I guess the missing bit in your interface definition is `fun` keyword. This is required for SAM conversions to work.

> **[Functional (SAM) interfaces | Kotlin](https://kotlinlang.org/docs/fun-interfaces.html)**

---

<div class="post-metadata">

**Author:** ![kasisoft](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kasisoft/32/9397_2.png) [@kasisoft](https://discuss.kotlinlang.org/u/kasisoft)\
**Post date:** [June 29, 2022, 10:33pm UTC](https://discuss.kotlinlang.org/t/sam-functional-interfaces-and-interoperability-with-java/25118/3 "2022-06-29T22:33:44Z")

</div>

Thanks.  
I read that in an article which wasn’t as clear as the documentation on your link. However this clearly indicated that I did something wrong as I wasn’t missing the **fun** keyword.  
Investigating the class with **javap** I figured that the generated class had two abstract methods which lead me to the **missing** ingredient: The compiler option **-Xjvm-default=all**.

Problem solved.
