Delegation


We need inheritance of more than one activity in base activity. It is necessary to combine them, but there is no multiple inheritance in kotlin. What are we gonna do?

Delegation is a design pattern. we use this pattern with by. When we use by lazy or by viewModel, we use the concept of delegation.

The base activity doesn't know exactly what's inside the class. Therefore, responsibility is not very clean when inheritance is used. So we can use delegation in this case.

Delegation Is Not Inheritance! in Kotlin, we can delegate not only to interfaces but to separate properties as well.



Why do we use delegation?

  • It is a flexible, powerful as well as mutable method.
  • Multiple interfaces can be implemented with the help of the existing ones.
  • It is used to add new features and values to current implementations.





  • Code Example

    
    interface Nameable{
    	var name : String
    }
    										
    class AtillaName : Nameable{
    	override var name:String ="Atilla"
    }
    										
    class LongDistanceRunner: Runnable{
    	override fun run() {
    		println("too long ago")
    	}
    }
    										
    class People(name:Nameable,runnable: Runnable): Nameable by name, Runnable by runnable
    										
    	fun main(args: Array) {
    		val person = People(AtillaName(),LongDistanceRunner())
    		println(person.name)
    		person.run()
    	}
    
    
    Output: 
    Atilla
    too long ago
    



    INHERITANCE VS. DELEGATION


  • Delegation can be an alternative to inheritance.
  • Delegation means that you use an object of another class as an instance variable, and forward messages to the instance.
  • It doesn’t force you to accept all the methods of the super class
  • Run-time flexibility. The delegate can easily be changed at run-time.
  • Unlike inheritance, delegation is not directly supported by most popular object-oriented languages(like java). Kotlin supports it.


  • inheritance

    
    open class RealPrinter{  //open keyword is necessary for inheritance in kotlin unlike java
    	open fun print(){
    		println("printing data")
    	}
    }
    										
    class Printer : RealPrinter() {
    	override fun print(){
    		super.print() //parent's print func.
    	}
    }
    										
    fun main(args: Array) {
    										
    	val printer = Printer()
    	printer.print()
    										
    }
    
    
    Output: 
    printing data
    

    delegation

    
    open class RealPrinter{  //open keyword is necessary for inheritance in kotlin unlike java
    	open fun print(){
    		println("printing data")
    	}
    }
    							
    class Printer{
    	val p = RealPrinter()
    	fun print(){
    	p.print()
    		}
    }
    							
    fun main(args: Array) {
    							
    	val printer = Printer()
    	printer.print()
    							
    }
    
    
    Output: 
    printing data
    

    When to use what?

  • You want to be able to pass your class to an existing API expecting A’s then you need to use inheritance.
  • You want to enhance A, but A is final and can no further be sub-classed then you need to use composition and delegation.
  • You want functionality of a method and do not want to override the method then you should go for delegation.



  • -> Some good resources on the subject:

  • kotlinlang.org
  • geeksforgeeks(inheritance vs. delegate)
  • geeksforgeeks(delegation)
  • blog.frankel.ch