Kotlin gives the programmer the ability to add more functionality to the existing classes, by without inheriting them. This is achieved through a feature known as extensions. When a function is added to an existing class it is known as Extension Function.
To add an extension function to a class, define a new function appended to the classname as shown in the following example:
Area of the circle is 19.634954084936208 Perimeter of the circle is 15.707963267948966
Here, a new function is appended to class using dot notation with class
Circle.perimeter() and its return type is Double. In the main function an object is created to instantiate the class Circle and invoked the function in
println() statement. When the member function is invoked it returns the area of circle and similarly extension function returns the perimeter of circle.
Extended library class using extension function –
Kotlin not only allows the user-defined classes to be extended but also the library classes can be extended. The extension function can be added to library classes and used in a similar way as for user-defined classes.
The following example demonstrates an extension function created for a user-defined class-
Explanation: Here, we have extended the library function using an extension function. We performed the modulus operation on an Integer value. We have passed the integer value -4 and 4 and obtain positive value for both. If parameter value is less than 0 than returns -(value) and if parameter value is greater than zero returns the same value.
Extensions are resolved statically –
One important point to note about the extension functions is that they are resolved statically i.e which extension function is executed depends totally on the type of the expression on which it is invoked, rather than on the type resolved on the final execution of the expression at runtime.
The following example will make the above argument clear:
If you are familiar with Java or any other object-oriented programming language, you might notice in the above program, that since class B inherits class A and the argument passed display function is an instance of class B. The output should be 25 according to the concept of the dynamic method dispatch, but since the extension functions are statically resolved, so the operate function is called on type A. Hence the output is 10.
Nullable Reciever –
Extension functions can also be defined with the class type that is nullable. In this case, when the check for null is added inside the extension function and the appropriate value is returned.
Example of an extension function as a nullable receiver –
Name is Charchit Null
Companion Object Extensions –
If a class contain companion object, then we can also define extension functions and properties for the companion object.
Companion object declaration –
Function declared in companion object
Just like the calling of regular member function of the companion object, extension function can be called using only the class name as the qualifier.
Companion object extension example –
Function declared in companion object Extension function of companion object
- OOPS | Generalization as extension and restriction using Java
- How to remove extension from string in PHP?
- How to enable PHP's openssl extension to install Composer ?
- How to remove .html extension from URL of a static page ?
- Kotlin infix function notation
- Suspend Function In Kotlin Coroutines
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
- Kotlin Environment setup with Intellij IDEA
- Kotlin Nested class and Inner class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.