76. What is a Member Function in Kotlin?
A member function in Kotlin is a function that is defined inside a class or object. It operates on the instance of the class and can access its properties and other functions.
Example
class Person(val name: String, var age: Int) {
fun greet() {
println("Hello, my name is $name and I am $age years old.")
}
}
Here, greet() is a member function of the Person class.
1. Regular: Defined inside a class
2. Open / Override: For inheritance
3. Abstract: Must be implemented in subclasses
4. Companion Object Functions: Similar to static
5. Extension Functions: Defined outside but act like members (syntactic sugar)
77. What is Top-level Functions in kotlin ?
In Kotlin, top-level functions are functions that are declared outside any class, object, or interface. They are defined directly in a Kotlin file, not wrapped in a class like in Java.
Example
// File: Utils.kt
fun greet(name: String) {
println("Hello, $name!")
}
You can call this function from anywhere in your code:
greet("Alice")
No need to create an object or class like in Java.
Comparision
Feature | Kotlin (Top-Level) | Java (Traditional) |
---|---|---|
Syntax | fun greet(name: String) | public static void greet(...) |
Belongs to | File | Class |
Needs class/object? | ❌ No | ✅ Yes |
Java interoperability | ✅ With @JvmName | — |
78. The difference between extension functions and member functions in Kotlin
Here is a comparison table showing the key differences between Extension Functions and Member Functions in Kotlin:
Feature | Member Function | Extension Function |
---|---|---|
Definition Location | Defined inside a class | Defined outside the class |
Access to private members | ✅ Yes – full access to all members of the class | ❌ No – can access only public/protected members |
Use of this | Refers to the current object of the class | Refers to the receiver object of the extension |
Inheritance | ✅ Supports overriding via open and override | ❌ Cannot be overridden – behaves like static functions |
Extension Target | Only to the class it belongs to | Can be added to any existing class, even Java classes |
Code Modification | Requires modifying the class definition | Can extend classes without modifying their source code |
Polymorphism | ✅ Fully supported | ❌ Not supported (resolved statically, not at runtime) |
Best Use Case | When functionality is part of the class's core logic | When adding helper functions or utilities externally |
Example | fun greet() { ... } inside a User class | fun User.greet() { ... } defined outside the User class |
79. Member vs Top-level Functions
Feature | Member Function | Top-level Function |
---|---|---|
Declared inside | A class or object | Outside of any class |
Called from | Object instance | Directly by name or package |
Has access to | Class properties | Only what is in scope |
80. The concept of property access in Kotlin.
In Kotlin, property access refers to how variables (properties) are read and written, and how you can customize or intercept those operations using:
1. Default Property Access
var name: String = "John"
This is equivalent to:
var name: String = "John"
get() = field
set(value) {
field = value
}
field is a backing field used to hold the actual value.
You can override the default behavior.
var age: Int = 18
get() = field + 1 // Custom getter adds 1
set(value) {
field = if (value < 0) 0 else value
}
Output:
age = -5 // Sets to 0 (setter logic)
println(age) // Prints 1 (getter logic)
3. Read-only Properties (val)
val id: Int = 42
Equivalent to:
val id: Int
get() = 42
val only has a getter, no setter.
4. Delegated Property Access
val username: String by lazy { "user123" } // Read access is delegated
You can also observe and veto changes with:
var count: Int by Delegates.observable(0) { prop, old, new ->
println("${prop.name} changed from $old to $new")
}
5. Backing Properties
Used when you want to control access to a property, but store the data in a different field.
private var _email = "test@example.com"
val email: String
get() = _email // Expose only getter
Useful for encapsulation.
Importtant Table
Feature | Description |
---|---|
get() | Custom read logic |
set() | Custom write logic |
field | Backing field keyword |
val | Read-only property (getter only) |
var | Read-write property (getter + setter) |
by lazy | Lazy initialization |
Delegates.observable | Trigger on value change |
Backing property | Used for encapsulation and logic split |