avatarGabriel Shanahan

Summary

The provided content offers a concise summary and practical examples of delegation in Kotlin, covering both interface and property delegation.

Abstract

The web content titled "Delegation — Summary" provides a succinct recap of delegation patterns in Kotlin, emphasizing its application in delegating interface implementations and property getters and setters. It highlights the use of the by keyword for delegation and illustrates this concept through Kotlin Playground code examples. The article is part of the "Kotlin Primer," intended to facilitate Kotlin adoption in Java-centric organizations, and includes a call to action for readers to explore further through the "Kotlin Primer" series.

Opinions

  • The author suggests that understanding delegation in Kotlin may initially be challenging but is essential for effective language usage.
  • Delegation is presented as a powerful feature in Kotlin, enhancing code reuse and maintainability.
  • The article expresses gratitude to Etnetera a.s. for supporting the creation of the "Kotlin Primer" series, indicating a collaborative effort in knowledge sharing.
  • It is recommended to read the "Introduction" before proceeding with the rest of the "Kotlin Primer" articles, suggesting a structured learning path for readers.
  • The inclusion of interactive Kotlin Playground examples demonstrates a commitment to providing hands-on learning resources.
  • The article concludes with a subtle invitation to join the team at Etnetera, showcasing the company's involvement in the Kotlin community and potential career opportunities.

Delegation — Summary

A quick recap of delegation in Kotlin — delegating interface implementations and delegating properties.

— — — — — — — — — — — — — — —

THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.

— — — — — — — — — — — — — — —

Tags: #FUNDAMENTAL CONCEPT

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.

It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.

When you’re new to delegation, it usually takes some time to wrap your head around the concept, and there is occasionally some confusion about which things can be delegated. So let’s go through a quick summary of the topic.

There are 2 ways delegation can be used in Kotlin

  • delegating implementations of interfaces
  • delegating implementations of properties (getters, setters)

When delegating implementations of interfaces, the by keyword is used in the class declaration after each interface whose implementation we wish to delegate. An instance of a type implementing the interface in question must follow:

When delegating implementation of properties, the by keyword is used in the property declaration after the name of the property whose implementation we wish to delegate. The property need not be a class property, and can also be a property that’s declared as part of a function, or at the top-level.

We must specify either:

  • an instance of a type implementing methods getValue (and setValue for var's)
  • another property (with some limitations)

Go back to Delegated Properties, jump to the Table of Contents, or continue to Sealed Hierarchies: Introduction.

Join me in Etnetera
Kotlin
Java
Programming
Object Oriented
Delegation
Recommended from ReadMedium