Computer scienceProgramming languagesKotlinObject-oriented programmingClass hierarchy

Functional (SAM) interfaces

Implementing a Functional Interface for Addition in Kotlin

Report a typo

Here's what you need to do for this task:

  1. Create a functional interface named Sum. This interface should declare a method called add that takes two integer parameters, a and b, and returns an integer.

  2. Use a lambda expression to implement this interface in the main function. This lambda expression should take two arguments and return their sum.

  3. Inside the main function, use the add method from the Sum interface with the numbers 5 and 3. Next, show the result on the console.

Reorder lines using drag or arrows. Adjust indentation with left buttons
                }
              
                val sum: Sum = Sum { a, b -> a + b }
              
                }
              
                fun add(a: Int, b: Int): Int
              
                fun interface Sum {
              
                println(sum.add(5, 3))
              
                fun main() {
              

Create a free account to access the full topic