https://kotlinlang.org logo
#getting-started
Title
# getting-started
d

Dron Bhattacharya

02/24/2023, 3:02 AM
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

Chris Lee

02/24/2023, 3:07 AM
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

Dron Bhattacharya

02/24/2023, 3:12 AM
So,
implementation
is not equivalent to
inheritance
??
c

Chris Lee

02/24/2023, 3:14 AM
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

Dron Bhattacharya

02/24/2023, 3:15 AM
And, for delegation to work, both the concerned classes should implement a common interface???
c

Chris Lee

02/24/2023, 3:17 AM
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

Dron Bhattacharya

02/24/2023, 3:19 AM
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

Chris Lee

02/24/2023, 3:20 AM
Yes. Methods on A can delegate some or all of their responsibilities to methods on B.
d

Dron Bhattacharya

02/24/2023, 3:23 AM
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

Chris Lee

02/24/2023, 3:25 AM
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

Dron Bhattacharya

02/24/2023, 3:26 AM
OK Now it is much clear. Thanks a lot.
👍 1
5 Views