Kotlin Extension Function - Javatpoint
Kotlin Extension Function - Javatpoint
Kotlin Tutorial
Kotlin Tutorial
Environment Setup
Hello World Program
First Program Concept
Environment Setup (IDE)
First Program (IDE) Kotlin Extension Function ← Prev Next →
Kotlin Variable
Kotlin Data Type Kotlin extension function provides a facility to "add" methods to class without inheriting a class or
Kotlin Type Conversion using any type of design pattern. The created extension functions are used as a regular function inside
Kotlin Operator that class.
Kotlin Input/Output
Kotlin Comment The extension function is declared with a prefix receiver type with method name.
Control Flow Triggers in SQL (Hindi)
⇧Return
SCROLLand TO
Jump
TOP
Continue Structure
Example of extension function declaration and its use
Function
In general, we call all methods from outside the class which are already defined inside the class.In below
Kotlin Function
example, a Student class declares a method is Passed() which is called from main() function by creating
Recursion Function
Default and Named Argument the object student of Student class.
Kotlin Lambdas
Higher order Function
Kotlin Inline Function
Array
Kotlin Array
String
Kotlin String
Exception Handling
Exception Handling
Kotlin Try Catch
Multiple Catch Block
Nested Try Block
Kotlin Finally Block
Kotlin Throw Keyword Suppose that we want to call a method (say isExcellent()) of Student class which is not defined in class. In
Null Safety such situation, we create a function (isExcellent()) outside the Student class as Student.isExcellent() and
Nullable Non Nullable Types call it from the main() function. The declare Student.isExcellent() function is known as extension
Kotlin Smart Cast function, where Student class is known as receiver type.
Unsafe and Safe Cast
Kotlin Elvis Operator
class Student{
Collections
fun isPassed(mark: Int): Boolean{
Mutable Array
Kotlin Collections return mark>40
List: listOf() }
mutableListOf()
}
Kotlin ArrayList
fun Student.isExcellent(mark: Int): Boolean{
arrayListOf()
Map: mapOf() return mark > 90
Kotlin HashMap }
hashMapOf()
fun main(args: Array<String>){
mutableMapOf()
val student = Student()
Set: setOf()
mutableSetOf() val passingStatus = student.isPassed(55)
hashSetOf() println("student passing status is $passingStatus")
Annotations
Kotlin Annotations
val excellentStatus = student.isExcellent(95)
Reflection println("student excellent status is $excellentStatus")
Kotlin Reflection
}
Kotlin OOPs
Class and Object
Output:
Nested and Inner Class
Kotlin Constructor
Visibility Modifier student passing status is true
Kotlin Inheritance student excellent status is true
Abstract Class
Kotlin Interface
Data Class The above example only demonstrates about how to declare an extension function.
Sealed Class
Extension Function Kotlin extension function example
Kotlin Generics
Let's see the real example of extension function. In this example, we are swapping the elements of
Ranges
Integer type range
MutableList<> using swap() method. However, MutableList<>class does not provide the swap() method
Kotlin Working Ranges internally which swap the elements of it. For doing this we create an extension function for
Kotlin Utility Function MutableList<> with swap() function.
Java Interoperability
The list object call the extension function (MutableList<Int>.swap(index1: Int, index2:
Calling Java code from Kotlin
Calling Kotlin code from Java Int):MutableList<Int>) using list.swap(0,2) function call. The swap(0,2) function pass the index value of list
Regex
inside MutableList<Int>.swap(index1: Int, index2: Int):MutableList<Int>) sxtension function.
Output:
A class which contains companion object can also be defined as extension function and property for the
companion object.
In this example, we call a create() function declared inside companion object using class name (MyClass)
as qualifier.
class MyClass {
companion object {
fun create():String{
return "calls create method of companion object"
}
}
}
fun main(args: Array<String>){
val instance = MyClass.create()
}
Output:
Sponsored
Let's see an example of companion object extensions. The companion object extension is also being
called using the class name as the qualifier.
class MyClass {
companion object {
fun create(): String {
return "calling create method of companion object"
}
}
}
fun MyClass.Companion.helloWorld() {
println("executing extension of companion object")
}
fun main(args: Array<String>) {
MyClass.helloWorld() //extension function declared upon the companion object
}
Output:
← Prev Next →
Feedback
Preparation
Company
Interview
Questions
Company Questions
Trending Technologies
B.Tech / MCA
Sponsored
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on hr@javatpoint.com, to get more information about given services.
Website Designing
Website Development
Java Development
PHP Development
WordPress
Graphic Designing
Logo
Digital Marketing
On Page and Off Page SEO
PPC
Content Development
Corporate Training
Classroom and Online Training
Data Entry
Learn Java Javatpoint.com Website Development Address: G-13, 2nd Floor, Sec-3
Learn Data Structures Hindi100.com Android Development Noida, UP, 201301, India
Learn C Programming Lyricsia.com
Website Designing Contact No: 0120-4256464, 9990449935
Learn C++ Tutorial Quoteperson.com
Learn C# Tutorial Jobandplacement.com Digital Marketing Contact Us
Learn PHP Tutorial Summer Training Subscribe Us
Learn HTML Tutorial Privacy Policy
Industrial Training
Learn JavaScript Tutorial Sitemap
Learn jQuery Tutorial College Campus Training
Learn Spring Tutorial About Me