It has been true for years and remains so today that almost every significant software system you build with Object Orientated Programming (OOP) uses inheritance for code reuse. Similar to the inheritance pattern, the delegation pattern in object-oriented programming permits code reuse. In this tutorial on Kotlin delegation, you will learn different topics of Delegation in Kotlin with examples.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

What Is Kotlin Delegation?

Making a composition as powerful for reuse as inheritance is possible with Delegation. Two objects under Delegation deal with a request: a receiving object delegates tasks to its delegate, who then handles the request.

Delegation governs the transfer of authority or power from one object to another. They are applicable to both static and dynamic relationships between classes and functions once they have been implemented.

Now, have a look at its syntax:

Kotlin_Delegation_1.

Delegation techniques allow for changing how inheritance is implemented in classes and functions, and object-oriented programming languages naturally support this without the need for boilerplate code. The by keyword in Kotlin is used to achieve this.

In the above example, Boss is the name of the interfaces, and run() is its abstract function. The by keyword is used from DerivedClass to access the run() function, which is implemented in BaseClass.

Now, you will understand how Kotlin Delegation works.

How Does Delegation Work in Kotlin?

Like Java and other languages, the Kotlin language features a large number of design patterns. Each design pattern has its own logic, which helps simplify the code. 

Kotlin has added a new keyword called "by" to support the "delegation" design pattern. The derived class can access all the implemented public methods of an interface through a particular object using Kotlin's delegation or keyword techniques.

Now, you will understand the types of Kotlin Delegation.

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

Types of Delegation

There are two types of Delegation in Kotlin.

Explicit delegation: This delegation is supported by all object-oriented language, and it is done by providing the delegating object with a delegate object.

Implicit delegation: In this delegation, language-level support for the delegation pattern is necessary.

Kotlin_Delegation_2

In this example, if you add the override fun run() println("Hello there!") to DerivedClass when run is called, the program will print Hello there! Instead of Hi there!.

Mentioned below is the output:

Kotlin_Delegation_3

Property Delegation

Delegation means transferring ownership to a different class or function. You should reuse the same code to initialize properties when it has already been declared in other places. Here you will look at the methods the Kotlin Library offers.

Lazy()

Lazy is a lambda function that accepts a property as an argument and returns an instance of Lazy<T>, where <T> is essentially the type of the properties it is utilizing.

Kotlin_Delegation_4

The Lazy function in the code above receives the variable myVar, assigns a value to it, and then returns that value to the main function.

Depicted below is the output.

Kotlin_Delegation_5

Delegates.observables()

Observable() initializes the object with two arguments and returns the same to the called method. 

Kotlin_Delegation_6

Here, in the example, the initial value of the property is the first argument. The callback function that will print oldName and newName is the second argument. This will be called when you alter the value of the specified property.

Below is the output of the above example.

Kotlin_Delegation_7

Now, you will move on to the next topic of Kotlin delegation.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Storing Properties in a Map

One common use case is keeping property values on a map. This commonly occurs in applications like parsing JSON or carrying out other dynamic operations. Here, the map instance itself can serve as the delegate for a delegated property.

Kotlin_Delegation_8

As you can see below, the constructor is taking the map.

Kotlin_Delegation_9.

Delegated properties use string keys associated with the names of properties to get values from this map. Below is the output.

If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!

Conclusion

In this tutorial on Delegation in Kotlin, you understood how delegation works in Kotlin and the types of delegation in Kotlin. You also learned about the Property Delegation Lazy() and Delegates. observables() function. Additionally, you learned about storing properties in a map and much more in detail with the help of examples. 

If you are looking to build a software development career, you can check the Post Graduate Program in Full Stack Web Development by Simplilearn. It might be the best option for assisting you in advancing your profession.

Do you have any questions about the Kotlin delegation tutorial? If you do, you can include them in the comments section below. Your questions will be addressed by us at the earliest.

About the Author

Harsh BhardwajHarsh Bhardwaj

Harsh is a Research Analyst at Simplilearn. He has a good hand at C++, Java, CSS, SQL, and a good grasp of Kotlin language. Harsh is quite interested in traveling, spirituality, and playing football.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors