Junior
What is the difference between a regular method and an extension method in Kotlin?
sobes.tech AI
Answer from AI
A regular method is declared inside a class and operates on the data of that class.
An extension method allows you to "add" a method to an existing class, including third-party libraries, without modifying its source code. It is declared outside the class and has a special syntax: the name of the extended type, a dot, and the method name.
class MyClass {
fun regularMethod() {
// Operates on MyClass data
}
}
fun MyClass.extensionMethod() {
// Has access to this - the instance of MyClass
}