13 - Kotlin Extension Function
13 - Kotlin Extension Function
Kotlin gives the programmer the ability to add more functionality to the
existing classes, 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:
Kotlin
package kotlin1.com.programmingKotlin.chapter1
Output:
Here, a new function is appended to the 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 a circle
and similarly, the extension function returns the perimeter of the circle.
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 library
class-
Kotlin
fun main(){
println((-4).abs())
println(4.abs())
}
Output:
4
4
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:
Kotlin
// Class B inherits A
class B():A(5, 5){}
fun main(){
Output:
10
Explanation:
Nullable Receiver
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 –
Kotlin
fun main(){
// An extension function as a nullable receiver
fun AB?.output(){
if(this == null){
println("Null")
}else{
println(this.toString())
}
}
val x = AB("Charchit")
Output:
Name is Charchit
Null
Kotlin
class MyClass {
// companion object declaration
companion object {
fun display(){
println("Function declared in companion object")
}
}
}
fun main(args: Array<String>) {
// invoking member function
val ob = MyClass.display()
}
Output:
Kotlin
class MyClass {
companion object {
// member function of companion object
fun display(str :String) : String{
return str
}
}
}
// extension function of companion object
fun MyClass.Companion.abc(){
println("Extension function of companion object")
}
fun main(args: Array<String>) {
val ob = MyClass.display("Function declared in companion object")
println(ob)
// invoking the extension function
val ob2 = MyClass.abc()
}
Output:
Similar Reads
How to Write swap Function Working with Anonymous
in Kotlin using the also Function in Kotlin
Function?
Related Tutorials
Spring MVC Tutorial Spring Boot Tutorial
Previous Next
Article Contributed By :
CharchitKapoor
C CharchitKapoor