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
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
Yup that's a great explanation and happy to hear this video helped you!
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() { ... }
Finally an explanation that makes sense! Thanks a lot man..
Thanks Bola, I’m happy this explanation worked for you 😌
Simple and to the point explanation
Thanks Jawad, happy to hear this was helpful!
Great explanation!!
Glad it was helpful!
Super! Thank you very much
You’re welcome 😊
Great Job! I would like to listen SOLID from you as well!
Thanks! SOLID is a good one that I’d love to do a video series on.
your video is help me so much, thanks!
You’re welcome! Happy to hear it helped 🙂
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.
it was really helpful for me! thanks!
Thanks it really helped me understand.
Yeah you’re welcome, happy to hear it helped ☺️
good explanation
Thank you :)
Thanks a lot!!🤗
Can somebody explain me with a more practical example what delegation is?
So it's multiple inheritance?
Yeah, basically multiple inheritance through composition with interfaces.
So what is the difference between class Cat: CanMove by FastMover() {...} and class Cat: FastMover {...}
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.
you don't wanna expose that your Cat is specifically a FastMover (as in, FastMover shouldn't be an exposed type)
Good video. However, you spelt Carnivore wrong. IntelliJ was saying you spelt it wrong.
I never was very good at spelling 😅
Honestly...C# is more simple to implement delegate....