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.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software 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 in web development.

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.

Learn 15+ In-Demand Tools and Skills!

Automation Testing Masters ProgramExplore Program
Learn 15+ In-Demand Tools and Skills!

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.

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

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.

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 Full Stack Developer - MERN Stack 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.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449