Search⌘ K
AI Features

Extension Functions vs. Member Functions

Explore the differences between Kotlin extension functions and member functions to understand how extensions can enhance classes without modifying them. Learn their behavior related to imports, virtual dispatch, and usage on nullable or generic types, enabling you to write clearer and more flexible Kotlin code.

Members and multiplicity

The biggest difference between members and extensions in terms of use is that extensions need to be imported separately. For this reason, they can be located in a different package. This fact is used when we cannot add a member ourselves. It is also used in projects designed to separate data and behavior. Properties with fields need to be located in a class, but methods can be located separately as long as they only access the public API of the class.

Multiple extensions with the same name

...