My understanding: We are using delegation to avoid...
# getting-started
d
My understanding: We are using delegation to avoid inheritance wherever possible. But if the main goal is to avoid inheritance why are we using Inheritance when delegating classes??? Example:
Copy code
interface IMoving {
    fun move()
}

class Walking : IMoving {
    override fun move() {
        println("Walking")
    }
}

class Animal(private val movable: IMoving) : IMoving {

    override fun move() {
        movable.move()
    }
}

fun main() {
    var movable = Walking()
    var animal = Animal(movable)
    animal.move()
}
Here we are still extending
IMoving
with
Animal
even though we are saying we don't want to do subclassing like this. In this way, we are also not able to limit the usage of methods when required
c
in that example both Animal and Walking implement the IMoving interface - no inheritance is involved. If you wish to limit the surface area of an existing interface there are two options: 1. Implement unsupported methods to throw an error, e.g.
TODO("Not implemented")
. This is ok in a limited set of scenarios; biggest drawback is lack of compile-time type safety (exceptions not uncovered until runtime) 2. Create a separate type that implements just the operations required. This can delegate (entirely, or in part) to other implementations.
d
So,
implementation
is not equivalent to
inheritance
??
c
correct. With interfaces (‘implements’) you are implementing an abstract concept and must fully provide the implementation. With inheritance you inherit the existing implementation and can selectively override. Use of inheritance is limited as it has many drawbacks; hence modern language like Kotlin have the ability to use composition (by delegation or other means).
d
And, for delegation to work, both the concerned classes should implement a common interface???
c
For Kotlin
by
delegation, yes, you are saying “I implement this interface, with calls forwarded to this delegate”. You an do delegation in other ways as well (it’s a general pattern of delegating certain operations to other parties). If your interface is too big consider splitting it up into focused responsibilities; classes can then implement one or more of those interfaces as appropriate.
d
But is a common interface necessary? if I have two classes A and B (no common interface). Can I just delegate a job to B from A where A holds a reference to B?
c
Yes. Methods on A can delegate some or all of their responsibilities to methods on B.
d
My understanding: I should use
by
for delegating classes only when I want to delegate all methods. If I want to limit/restrict some methods,
by
is not useful. In a case like this, I should do delegation as I said for classes A and B.
c
Close.
by
delegation requires implementing an entire interface, which by default has all methods forwarded to a delegate - you can selectively override methods. If you are wish to expose a different interface you can do as above for classes A and B.
K 1
d
OK Now it is much clear. Thanks a lot.
👍 1