Kotlin Class Functions
Kotlin Class Functions
Class functions (also called member functions) are functions that belong to a class and define the behavior of objects.
In Kotlin, class functions are clean, concise, and powerful.
1. Basic Class Function
2. Class Function with Parameters
3. Class Function with Expression Body
4. Accessing Class Properties in Functions
5. this Keyword in Class Functions
Used to refer to the current object.
6. Function Overloading in Classes
Same function name, different parameters.
7. Visibility Modifiers in Class Functions
| Modifier | Access |
|---|---|
public |
Everywhere (default) |
private |
Within class |
protected |
Class & subclasses |
internal |
Same module |
8. Open & Override Functions (Inheritance)
Functions are final by default.
9. Abstract Functions
10. Companion Object Functions (Static-like)
Usage:
11. Inline Class Functions
12. Extension Functions (Related to Classes)
Usage:
13. Real-World Example
Summary
-
Class functions define object behavior
-
Support overloading & overriding
-
Visibility modifiers control access
-
Companion objects replace static methods
-
Clean syntax compared to Java
