The purpose of extension functions is to add new functions to the existing class which means it can simply add functions to the class without declearing it inside the class. The new function actually behaves like static. It can become part of your own class (like Student) or can become part of the predefined class(like String, Int, Array etc). This function reduces the code and code become much easier and cleaner to read.
Ex–
fun main(args, Array<String>){
var student = Student()
println(“Passed status : ” + student . HasPassed(57))
println(“Scholar status : ” + student . isScholar(57))
}
fun Student . isScholar(marks : Int) : Boolean{ //Extension of your own class
return marks > 95
}
class Student{ // Your own Class
fun hasPassed(marks : Int) : Boolean{
return marks > 40
}
}
Output :
Passed status : true
Scholar status : false
Android Developer at Openweb Solutions