Delegation controls the allocation of power/authority from an instance to another for any object. For classes and functions implementations, delegations can be used on static as well as mutable relations between them. Inheritance implementation in classes and functions can be altered with the help of delegation techniques and object-oriented programming languages support it innately without any boilerplate code. Delegation is used in Kotlin with the help of “by” keyword.
There are two types of delegation present in Kotlin:
• Explicit delegation: Supported by all object-oriented language and it is done by passing a delegate(the one to be implemented) object to delegating object (the one that will implement delegate object).
• Implicit delegation: Requires language-level support for the delegation pattern.
Let us discuss the concept of the delegation with the help of the examples:
Example 1:
As we know that in Kotlin, inheritance provides us with a permanent static relationship between objects which are not mutable while delegation is, this fact makes Delegation an extremely powerful alternative. In this example, using Newfeature class we can implement delegation base class with new features by delegating all its public members i.e mymessage and messageline and we are using this implementation with the help of “by” keyword.
Kotlin
// Kotlin program to illustrate the
// concept of delegation
interface delegation
{
fun mymessage()
fun mymessageline()
}
class delegationimplementation(val y: String) : delegation
{
override fun mymessage()
{
print(y)
}
override fun mymessageline()
{
println(y)
}
}
class Newfeature(m: delegation) : delegation by m
{
override fun mymessage()
{
print("GeeksforGeeks")
}
}
// Main function
fun main()
{
val b = delegationimplementation("\nWelcome, GFG!")
Newfeature(b).mymessage()
Newfeature(b).mymessageline()
}
Output:
GeeksforGeeks
Welcome, GFG!
Example 2:
In this example, we have one delegation base class with val value and method “fun message()”. In the delegationimplementation class, we are assigning value to this “fun message” and later from another class we are using this implementation using “by” keyword to add a new statement with same val value;
Kotlin
// Kotlin program to illustrate the
// concept of delegation
interface delegation
{
val value: String
fun mymessage()
}
class delegationimplementation(val y: String) : delegation
{
override val value = "delegationimplementation y = $y"
override fun mymessage()
{
println(value)
}
}
class Newfeatures(a: delegation) : delegation by a
{
override val value = "GeeksforGeeks"
}
fun main()
{
val b = delegationimplementation("Hello!GFG")
val derived = Newfeatures(b)
derived.mymessage()
println(derived.value)
}
Output:
delegationimplementation y = Hello!GFG
GeeksforGeeks
Advantages:
1. It is a flexible, powerful as well as mutable method.
2. Multiple interfaces can be implemented with the help of the existing ones.
3. It is used to add new features and values to current implementations.
Similar Reads
Idioms in Kotlin Data classes in Kotlin are classes created to do nothing but hold data. Such classes are marked as data: data class User(var firstname: String, var lastname: String, var age: Int) The code above creates a User class with the following automatically generated: Getters and Setters for all properties (
4 min read
Delegated Properties in Kotlin Delegation is defined as the granting of any authority or power to another person (Boss assigning tasks to its employees) to carry out different work. However, the person who delegated the work remains accountable for the outcome of the delegated work. In a similar manner, there are various types of
5 min read
Kotlin Collections In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including:Collection NameDescriptionLists Ordered collections of elements that allow duplicates.Sets Unordered collections of unique elements.Maps Collection
6 min read
Kotlin Collections In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including:Collection NameDescriptionLists Ordered collections of elements that allow duplicates.Sets Unordered collections of unique elements.Maps Collection
6 min read
Kotlin Collections In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including:Collection NameDescriptionLists Ordered collections of elements that allow duplicates.Sets Unordered collections of unique elements.Maps Collection
6 min read
Kotlin Collections In Kotlin, collections are used to store and manipulate groups of objects or data. There are several types of collections available in Kotlin, including:Collection NameDescriptionLists Ordered collections of elements that allow duplicates.Sets Unordered collections of unique elements.Maps Collection
6 min read