avatarGabriel Shanahan

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1602

Abstract

nal interface, or a Single Abstract Method (SAM) interface. The functional interface can have several non-abstract members but only one abstract member.</p> <figure id="4072"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FStLgcu-VK&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FStLgcu-VK&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure></iframe></div></div></figure><p id="f4c6">Interfaces such as this one are basically equivalent to lambda functions sharing the same signature as the abstract member. Kotlin allows this for interfaces marked with the <code>fun</code> keyword.</p> <figure id="ca7f"> <div> <div> <img class="ratio" src="http://placehold.it/16x9"> <iframe class="" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fpl.kotl.in%2FVR-f1ON2o&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fpl.kotl.in%2FVR-f1ON2o&amp;image=https%3A%2F%2Fplay.kotlinlang.org%2Fassets%2Fog-image.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" allowfullscreen="" frameborder="0" height="300" width="800"> </div> </div> </figure><

Options

/iframe></div></div></figure><p id="fc70">If you’re curious as to why the decision was made to make a special keyword necessary, and interesting view was given by Pavel Talanov in the <a href="https://youtrack.jetbrains.com/issue/KT-7770#focus=Comments-27-997598.0-0">corresponding issue</a>:</p><blockquote id="03a2"><p><i>SAM are mainly a convenient automatic wrapper for the code that you cannot change. If you’re designing an API in Kotlin you can (and should) make a choice: Is this entity just a function or a separate class that can evolve into something more complex.</i></p></blockquote><p id="fa69">In other words, <a href="https://readmedium.com/preface-a65cb535d122#39fa">communicate intent</a>.</p><p id="734c">The same functionality is <a href="https://kotlinlang.org/docs/java-interop.html#sam-conversions">available</a> by default when calling Java classes.</p><p id="c99c">Go back to <a href="https://readmedium.com/inline-value-classes-773093aece23">Inline (Value) Classes</a>, jump to the <a href="https://readmedium.com/table-of-contents-c52573cfa291">Table of Contents</a>, or continue to <a href="https://readmedium.com/extension-functions-motivation-a55abc644c5a">Extension Functions: Introduction</a>.</p><figure id="8ecd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*biBSB579iezsNvEQ_NMLBg.png"><figcaption><a href="https://www.etnetera.cz/prace-u-nas?utm_source=medium&amp;utm_medium=GabrielShanahan&amp;utm_campaign=KotlinPrimer&amp;utm_content=join-our-team&amp;utm_term=KotlinPrimer#pozice">Join me in Etnetera</a></figcaption></figure></article></body>

Functional (SAM) Interfaces

An introduction to functional interfaces, also call SAM interfaces, and an interesting view on why they exist.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FYI++

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

An interface with only one abstract method is called a functional interface, or a Single Abstract Method (SAM) interface. The functional interface can have several non-abstract members but only one abstract member.

Interfaces such as this one are basically equivalent to lambda functions sharing the same signature as the abstract member. Kotlin allows this for interfaces marked with the fun keyword.

If you’re curious as to why the decision was made to make a special keyword necessary, and interesting view was given by Pavel Talanov in the corresponding issue:

SAM are mainly a convenient automatic wrapper for the code that you cannot change. If you’re designing an API in Kotlin you can (and should) make a choice: Is this entity just a function or a separate class that can evolve into something more complex.

In other words, communicate intent.

The same functionality is available by default when calling Java classes.

Go back to Inline (Value) Classes, jump to the Table of Contents, or continue to Extension Functions: Introduction.

Join me in Etnetera
Kotlin
Java
Programming
Object Oriented
Functional Programming
Recommended from ReadMedium