Extension Methods


Extension Methods

Kotlin provides the ability to accommodate further activity without any existing classes. It also gains through a feature known as extensions. Adding a function to the current class is known as the extension activity. To add an extension activity to a class, define a new function added to the class.

The extended library class uses extension methods

Kotlin is allowed not only to expand defined classes but also can expand Library classes. Extension action is used in a similar way to the user-defined classes.

Extensions can be resolved statically - An important thing to note about extension activities is that they are legally resolved. It depends on the requesting expression that requests any extension activity.

The Nullable Receiver - The look can also define extension Steps. In this case, the appropriate value is returned when adding NULL within the extension activity.

The Companion Object Extensions - If there is a companion object in a class, we can define extension functions and properties for expansion activity and properties.

Declaring extensions as members - You can declare extensions for a class inside another class. There are multiple clearer receivers in such an extension - objects that can access the objects without a qualification. An example of a class declared the extension is called a dispatch receiver, and the extension method of the expansion type is called an extension behavior.

Note that the visibility:

Extensions use the same vision modifiers that the same scope is the same. For example, An extension announced on the upper floor of a file has access to other private top-level virtues in the same file. If an extension is declared outside the Riber type, it cannot access the recipient's personal or protective members.