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.
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:
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.
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.
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:
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 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.
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.
Observable() initializes the object with two arguments and returns the same to the called method.
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.
Now, you will move on to the next topic of Kotlin delegation.
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.
As you can see below, the constructor is taking the map.
Delegated properties use string keys associated with the names of properties to get values from this map. Below is the output.
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.