functional interface or SAM

4 0 0
                                        

SAM - means Single Abstract Method interface or a functional interface. This is an interface with just one abstract method in it but it is allowed to have more than one regular method with implementations inside. This is the same with the regular interface except the following: 

1. you need to add fun keyword before the interface keyword. 

2. It does not allow abstract properties

3. It contains only one abstract method.

syntax A:

fun interface Name{

    fun yourMethod()

    fun anotherMethodWithBody(){

        // statement

    }

}

syntax1 during invocation of SAM

val objectName = object: Name{

    override yourMethod(){

        println("Hello World")

     }

}

syntax2 during invocation of SAM

val objectName = Name{ println("Hello World") }

Note: The use of lambda shortens the call of SAM but you need to take of the requirements such as parameter and return type. The invocation of SAM in syntax1 is like creating an object class.

=====================

fun main(){

    val obj1 = object: BookInterface{

       override fun displayBookInfo(){

           println("Book of Slime season 1")

       }

    }

    obj1.dummyMessage()

    obj1.displayBookInfo()

    val obj2 = BookInterface { println("Book of Slime season 2") }

    obj2.dummyMessage()

    obj2.displayBookInfo()

}

fun interface BookInterface{

    val dummy: String

              get() = "BookInterface"

    fun displayBookInfo()

    fun dummyMessage(){

        println("This is the interface $dummy")

    }

}

=====================

Result:

This is the interface BookInterface

Book of Slime season 1

This is the interface BookInterface

Book of Slime season 2


Note: You can convert a regular interface to SAM as long as it is within the rules by using a regular function but you need to use a lambda in its parameter.

syntax B:

interface A{

    fun methodA()

}

fun A(lambdaName: () -> Unit): A = object: A{

    override methodA() = lambdaName()

}

Note: 

1. We create a function whose name is A which is the same name as the Interface A. The function has a lambda parameter whose name is lambdaName. The lambdaName has no parameter and it returns nothing. So, Unit was used since this is kotlin which is equivalent in java as void. 

2. The function A returns an interface A. So, instead of using the normal return statement, we just used an assignment operator. 

3. We equate the function to an object class whose extension is an interface A which it overrides the method of the interface A.

4. And lastly the function A parameter which is a lambda function was assigned to the overridden method of the interface A.

=====================

fun main(){

    val obj = BookInterface{ println("Book of Slime season 3") }

    obj.dummyMessage()

    obj.displayBookInfo()

}

interface BookInterface{

     val dummy: String

            get() = "BookInterface"

     fun displayBookInfo()

     fun dummyMessage(){

        println("This is the interface $dummy")

     }

}

fun BookInterface(block: () -> Unit): BookInterface = object: BookInterface{

    override fun displayBookInfo() = block()

}

====================

Result:

This is the interface BookInterface

Book of Slime season 3


Note: Don't use deprecated mode if you are still using the interface but if you won't be using your code and wanted to preserve it, you annotate your code with at deprecated mode.

/../ this is my at sign for now

syntax:

/../deprecated(message = "whatever message", level=DeprecationLevel.HIDDEN)

interface A{ // statement }

Note: The name of the code in the Intellij IDE will be cross out and will not be available for use.


Kotlin ProgrammingWhere stories live. Discover now