Sobes.tech
Junior — Middle

What elements or properties cannot be extended or supplemented using extensions?

sobes.tech AI

Answer from AI

In Kotlin, extensions allow adding new functions and properties to existing classes without inheritance. However, there are limitations:

  • Cannot add new fields (properties with backing fields). Extension properties in Kotlin are essentially syntactic sugar for getters and setters; they cannot store state.

  • Cannot override existing methods or properties of a class. Extensions do not modify the class but only provide external functions.

  • Cannot extend class constructors.

  • Cannot extend local or nested classes declared inside functions.

  • Cannot extend elements that are not classes, such as primitive types directly (although Kotlin wraps them in classes).

Thus, extensions cannot change the internal state of a class or its behavior that requires access to private members.