Extensions Function in Kotlin

Updated 28 February 2019

Save

What are Extensions Function?

Extensions function in kotlin provide the ability to extend the class with new functionality without extends the class. Extensions function do not modify class they extend the class and insert new functionality in the class.

How to write extension functions?

Let’s begin with very basic we know that to show a simple Toast in an Activity we have to write boilerplate code we have to pass context, message and the duration in makeText() method. Then the question arises how extension function can make our life easy so let’s start with Toast now I going to show you how we can extend the Activity class and add new functionality to just show toast anywhere in activity all we have to do just call showToast() method and pass the message.

In the above example, we have seen how extension function we can write to just show a Toast and call it in any activity in our application.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home