Kotlin Interfaces & Class Delegation | Kotlin Tutorial 2019

Поделиться
HTML-код
  • Опубликовано: 26 сен 2024
  • Kotlin interfaces and class delegation go together like peanut butter and jelly. In this video we walk through how interfaces differ from abstract classes. From there we show how class delegation works along with why it's more powerful and flexible than using traditional inheritance models.
    As always though, if you have any questions about Kotlin interfaces and class delegation please leave a comment below and I'll get back to you as soon as I can.
    --- Follow Me ---
    Twitter - / codyengeltweets
    Medium - / codyengel
    --- Resources ---
    Start From The Beginning: • Setting Up IntelliJ An...
    GitHub Repository: github.com/Cod...
    Kotlin Interfaces & Class Delegation Documentation: kotlinlang.org...
    --- Camera Gear ---
    Camera - amzn.to/2LM9Zt3
    Microphone - amzn.to/2LLv0Eb
    Lens - amzn.to/317tGPT
    Main Lights - amzn.to/2K2yHDH
    Back Lights - amzn.to/2Yw4HUI
    External Monitor - amzn.to/2YoM2Kl
    * Note: the above links are affiliate links.
    --- Desk Gear ---
    Monitor - amzn.to/2yjZPqR
    Headphones - amzn.to/2YBuo68
    Google Nest Hub - amzn.to/30Zuki7
    MacBook Pro 2019 - amzn.to/30YyuXy
    * Note: the above links are affiliate links.
    #kotlin #learnprogramming #programming

Комментарии • 31

  • @pablon3115
    @pablon3115 2 года назад +2

    Thanks for such a good explanation and example. What I understood is:
    Delegation allows having multiple implementations of an interface so then, the base class implementing that interface has to override the interface method. At this point it will be possible to make use of any of the implementations of the interface in the body of the overriden method of the base class and in Kotlin we can perform it from the class definition. Example:
    class ClassI : InterfaceX by ClassAThatImplementInterfaceX()
    class ClassJ : InterfaceX by ClassBThatImplementInterfaceX()
    I wish it can help others to understand it easier (although the example in the video is perfectly clear) and not to bog down the path xD

    • @CodyEngelCodes
      @CodyEngelCodes  2 года назад

      Yup that's a great explanation and happy to hear this video helped you!

  • @Bellatronix
    @Bellatronix Год назад

    Thanks for you explanation!
    It seams that the main purpose of class delegation in Kotlin was to overcome restriction of multiple inheritance in classes. Just like traits in PHP you can assemble target Kotlin class from pieces (i.e. various classes-delegates) using multiple inheritance of interfaces with "by" syntactyc shugar. So, instead of the following that won't compile
    class Cat: FastMover(), Carnivoure() { ... }
    we can make it working with class delegation feature
    class Cat: CanMove by FastMover(), CanEat by Carnivoure() { ... }

  • @bolafahmi
    @bolafahmi 4 года назад +5

    Finally an explanation that makes sense! Thanks a lot man..

    • @CodyEngelCodes
      @CodyEngelCodes  4 года назад +1

      Thanks Bola, I’m happy this explanation worked for you 😌

  • @jawadyousaf2475
    @jawadyousaf2475 4 года назад +2

    Simple and to the point explanation

    • @CodyEngelCodes
      @CodyEngelCodes  4 года назад

      Thanks Jawad, happy to hear this was helpful!

  • @syprosegwako8297
    @syprosegwako8297 Год назад

    Great explanation!!

  • @MuzefferPasazade
    @MuzefferPasazade 4 года назад +3

    Super! Thank you very much

  • @dnkilic
    @dnkilic 4 года назад +1

    Great Job! I would like to listen SOLID from you as well!

    • @CodyEngelCodes
      @CodyEngelCodes  4 года назад +2

      Thanks! SOLID is a good one that I’d love to do a video series on.

  • @nandityanuswatama4394
    @nandityanuswatama4394 4 года назад +1

    your video is help me so much, thanks!

    • @CodyEngelCodes
      @CodyEngelCodes  4 года назад

      You’re welcome! Happy to hear it helped 🙂

  • @misterjedu
    @misterjedu 4 года назад

    Thanks for the explanation. It makes more sense to me now why delegation/composition it's better than inheriting or using abstract classes
    However, from the example you gave, the delegation it's still a bit rigid. The cat should not enjoy the steak. It should have returned false.
    I've seen an example where that is taken care of in the constructor (In this case, the Cat constructor) to make it much more flexible, but I was confused with the explanation.
    I was hoping to get a better explanation here.

  • @GameOnStreamTV
    @GameOnStreamTV 4 года назад

    it was really helpful for me! thanks!

  • @anubhavsharma1335
    @anubhavsharma1335 3 года назад

    Thanks it really helped me understand.

    • @CodyEngelCodes
      @CodyEngelCodes  3 года назад +1

      Yeah you’re welcome, happy to hear it helped ☺️

  • @vikaspandey7053
    @vikaspandey7053 4 года назад +1

    good explanation

  • @wintersijan
    @wintersijan 3 года назад

    Thanks a lot!!🤗

  • @soma7891
    @soma7891 2 года назад

    Can somebody explain me with a more practical example what delegation is?

  • @donwald3436
    @donwald3436 2 года назад

    So it's multiple inheritance?

    • @CodyEngelCodes
      @CodyEngelCodes  2 года назад

      Yeah, basically multiple inheritance through composition with interfaces.

  • @OlivierGeorg
    @OlivierGeorg 3 года назад

    So what is the difference between class Cat: CanMove by FastMover() {...} and class Cat: FastMover {...}

    • @CodyEngelCodes
      @CodyEngelCodes  3 года назад

      The `by` keyword will use the classes interface definition for those interface definitions on the class. So your first example, Cat would use the FastMover implementation of CanMove while allowing you to implement other interfaces (using their own `by` keyword). Your second example would require FastMover to be `open` so it can be inherited from `Cat`, Kotlin only allows for inheriting from one parent class.

    • @notkamui9749
      @notkamui9749 2 года назад

      you don't wanna expose that your Cat is specifically a FastMover (as in, FastMover shouldn't be an exposed type)

  • @MirrorsEdgeGamer01
    @MirrorsEdgeGamer01 4 года назад

    Good video. However, you spelt Carnivore wrong. IntelliJ was saying you spelt it wrong.

  • @wisnu7734
    @wisnu7734 4 года назад

    Honestly...C# is more simple to implement delegate....