OOP Principles: Composition vs Inheritance

This video attempts to introduce the viewer to the two major Object Oriented Design paradigms. Composition and Inheritance. In this video, I use Python for my programming language. Much of the information here came from FunFunFunction's video on this topic at:
• Composition over Inher...

Пікірлер: 100

  • @DerEchteAndrej
    @DerEchteAndrej5 жыл бұрын

    Thank you Dave for this video. It's the best one on this topic I've seen so far, and it helped me to understand this eventually!

  • @tkvenki
    @tkvenki5 жыл бұрын

    One of the most Simple and Best explanations on this topic!!

  • @therobby3
    @therobby35 жыл бұрын

    Thanks for the video, very clear and the point. I've been using inheritance way too much over the years and this explains exactly what I've needed.

  • @new-anointingaremu3597
    @new-anointingaremu3597 Жыл бұрын

    Have actually watched about 10 videos for composition vs inheritance this is still the best

  • @davecrabbe4579

    @davecrabbe4579

    Жыл бұрын

    thx

  • @user-qy1ny9bu1h
    @user-qy1ny9bu1h4 жыл бұрын

    Finaly some good explanation what is composition! Other videos were about rooms, walls, furniture and windows...

  • @error467able
    @error467able2 жыл бұрын

    I am having difficulties understanding this topic, but this explained far way better than the other. Great job.

  • @davecrabbe4579

    @davecrabbe4579

    2 жыл бұрын

    This was a topic that gave most of my students difficulty.. so you are not alone.

  • @southern_smacker
    @southern_smacker4 жыл бұрын

    I was watching different videos on this subject since last 8 hours but could not understand inheritance and composition. The approach and language used in this video is pretty simple and readily understandable.

  • @surfer4987
    @surfer49875 жыл бұрын

    Provided a quick reference with some simple and easy to understand examples. Thanks a ton, helped me with my homework

  • @gintautasmartinaitis4789
    @gintautasmartinaitis47895 жыл бұрын

    You are amazing! The best video on this subject I have seen! Thank you!

  • @jonathan10543
    @jonathan105435 жыл бұрын

    Wow I hope you make more Python and Object Oriented Design videos! This is top-notch!

  • @rajivtipp
    @rajivtipp5 жыл бұрын

    Very good explanation, tried to read from many sites but this 15mn video is all you need.

  • @balamurugankalyanasundaram426
    @balamurugankalyanasundaram4264 жыл бұрын

    One of the clear explanations on composition..

  • @houseandhobby6971
    @houseandhobby6971 Жыл бұрын

    Short sweet simple concise thank you. And for such simplicity one gets rewarded with alternative ideas that forget that a simple somewhat contrived example-set is needed for the original goal. I suppose as an aside for a coder to guarantee no code duplication that the objects are done away with and that all unique referenced code exists in a static function library. And then any and every object grabs an a la cart copy of what it wants to implement.

  • @simonsantama
    @simonsantama3 жыл бұрын

    I liked this a lot. Super clear, well explained.

  • @aMulliganStew
    @aMulliganStew4 жыл бұрын

    Thank you. I get it now. What was missing in my understanding was having functions implemented as a call-thrus to functions of member objects. I'm not sure if this'll help me on my current project, but it does give me something else to consider. Again, thanks.

  • @shinkicked3686
    @shinkicked36865 жыл бұрын

    Why not just separate behaviors in different classes? like barker, mover, eater cleaner and you compose every object base on behavior like.. dog will be compose of eater and barker objects, CleanRobot will be composed of cleaner object and so on.. the end game with the SuperRobot will be an object composed of barker, cleaner, mover and gameplayer. I think that would be a better, more expressive and clean design and is even more reusable because what if later on the dog needs to move too.. it would be ugly to compose a dog with a robot in order to make it move, instead of you compose dog with mover object that makes more sense!.. But nice video!

  • @milad5554

    @milad5554

    4 жыл бұрын

    nice 👍

  • @Catalin52

    @Catalin52

    2 жыл бұрын

    This is the right approach 👍

  • @davecrabbe4579

    @davecrabbe4579

    Жыл бұрын

    yes.. I would tend to agree with this.. If starting from scratch this is a better way to proceed. Good thinking.

  • @ipodtouch470

    @ipodtouch470

    11 ай бұрын

    Why make stuff like barker, mover, and water classes though? Why not just make them interfaces and implement them? This would also give you the benefit of polymorphism which is imo one of the main advantages of inheritance.

  • @bro_chenzox
    @bro_chenzox4 жыл бұрын

    Thanks, you are the best explainer. I am not a native speaker, but there is something in your videos that is guaranteed to provide 100% absorption of information. Exactly those with a webcam.

  • @bro_chenzox

    @bro_chenzox

    4 жыл бұрын

    PS. I start to understand why I will never meet you in Google, there would be a high risk of overflow of good developers, then minimizing their salaries and totally development crisis ). Thank again, Dave!

  • @voltaralcala9021
    @voltaralcala90214 жыл бұрын

    I will be back here later, thank you for great explanation once again

  • @FXK23
    @FXK235 жыл бұрын

    I'm not into python but coupling a bot to a dog (dependency) in order to let the bot 'bark' doesn't seem the right thing to do. Abstracting out Skills classes/interfaces as 'in between models' would make more sense and decouples bots from dogs. Both animals and bots can then have some collection of skills to be invoked.

  • @yonishachar1887

    @yonishachar1887

    3 жыл бұрын

    can you give an example?

  • @MichaelLamTheDeveloper

    @MichaelLamTheDeveloper

    3 жыл бұрын

    Completely agreed, I knew what the video was trying to get at but the way the classes were used was weird. Using the example in the video, the SuperBot HAS A Dog, Robot and CleanRobot

  • @yonishachar1887

    @yonishachar1887

    3 жыл бұрын

    @@MichaelLamTheDeveloper Hey, can you please provide an example for what you'd consider a good refactor of the video?

  • @MichaelLamTheDeveloper

    @MichaelLamTheDeveloper

    3 жыл бұрын

    @@yonishachar1887 If SuperBot wants to bark, it shouldn't need to purchase a dog. As a SuperBot, it has a voice, I would create an interface called IVoice with a method called "Speak" inside. I would then create a concrete class that inherits from IVoice called "Bark". Within the Bark class I would implement the method "Speak" and return "bark bark!". So a SuperBot has a voice (IVoice), and when it "Speak" it'll say "bark bark!". I want to emphasize that SuperBot has a reference to "IVoice" and NOT the "Bark" class. This allows for polymorphism, we can change the Voice without changing the SuperBot class. Take it with a grain of salt, I'm learning as just as you are. I'm sure there are better approaches to this and hopefully, other members of the community can chime in. This is what I think would be better and clearer

  • @prototype8861

    @prototype8861

    3 жыл бұрын

    @@yonishachar1887 interfaces. ICanBark, ICanMove etc.

  • @Peter-bg1ku
    @Peter-bg1ku5 жыл бұрын

    Good video. Had to watch it at x1.5, though :)

  • @aMulliganStew

    @aMulliganStew

    4 жыл бұрын

    2x but that didn't bother me. I listen far more quickly than I speak.

  • @maksat607
    @maksat6075 жыл бұрын

    You are the best teacher!

  • @CaptainBravo87
    @CaptainBravo87 Жыл бұрын

    Amazing and simple!

  • @mattizie91
    @mattizie915 жыл бұрын

    I've struggled for years to understand OOP, this one video has really tied it together in a FAST and EFFECTIVE way that uses examples that demonstrate the concept beautifully. Very under-rated video.

  • @davecrabbe4579

    @davecrabbe4579

    5 жыл бұрын

    thank you for the comments

  • @angelsdemons8520
    @angelsdemons85203 жыл бұрын

    finally got the video that can explain the difference between inheritance and composition clearly , great job Mr. Dave

  • @LuisAguilar-hp3lq
    @LuisAguilar-hp3lq3 жыл бұрын

    Totally clear!! Thanks

  • @muddassirghoorun4322
    @muddassirghoorun43223 жыл бұрын

    Thanks for this awesome video. Good stuff!

  • @rahulagarwal7182
    @rahulagarwal71823 жыл бұрын

    Great VIdeo..How easily you explained it is awesome..Great work and Thanks for video

  • @sergiomoreno9223
    @sergiomoreno92234 жыл бұрын

    Thanks a lot, the best video of this topic.

  • @Aldabil
    @Aldabil3 жыл бұрын

    The real lesson I learned from this video is, people who create "this type of composition as explained in the video" clearly never heard about something called "abstraction".

  • @vineets176
    @vineets1763 жыл бұрын

    Made the concept clear. Keep it up.

  • @csquared9
    @csquared92 жыл бұрын

    Excellently described!!!

  • @milad5554
    @milad55544 жыл бұрын

    Well and simple described , Thank you

  • @aqilisayev5529
    @aqilisayev55293 жыл бұрын

    Thanks for explanation!

  • @z4vdi20
    @z4vdi206 жыл бұрын

    Really helped me. Thanks a lot!

  • @mattt2684
    @mattt26844 жыл бұрын

    Amazing explanation!

  • @TuanTran-jn5gs
    @TuanTran-jn5gs4 жыл бұрын

    Thank you so much Dave!!!

  • @sarahpes32
    @sarahpes323 жыл бұрын

    Thank you Soooo much!! Best explanation!!! 💙👍

  • @ahmetkarakartal9563
    @ahmetkarakartal95632 жыл бұрын

    thank you so much, this explanation is very clear

  • @nikokodzhabashev807
    @nikokodzhabashev8076 жыл бұрын

    Thank you! Amazing content

  • @arthurbrc_
    @arthurbrc_ Жыл бұрын

    "We need a robot that can clean, move, bark + play games..." 😂😂 That's what a boss would say

  • @seano.2267
    @seano.22674 жыл бұрын

    wow, i am the 10000th viewer. Great video, really helps, thank you.

  • @Mrmtl100
    @Mrmtl1003 жыл бұрын

    great explanation!

  • @cibelless510
    @cibelless5103 жыл бұрын

    very gooooooddddddd you are very clear

  • @sadam_khan
    @sadam_khan Жыл бұрын

    Brilliant.......

  • @machinimaaquinix3178
    @machinimaaquinix3178 Жыл бұрын

    Sept 2022 - This was a fantastic example of composition. You can't believe the abstract and academic word salads that are in various texts and some other YT vids. This explained the scenario so clearly, I finally got this, and on a day I got half as much sleep as normal lol. Regarding some other comments about a better way to do this by creating new specialist classes based on the actual functions, eg, "Bark", "Move" and "Clean" class ... - they are valid points but I think Dave was explaining this from the perspective of reusing existing classes - which is often a requirement, especially if you don't own or cannot modify the code. If one could re-model the domain, I would definitely use specialist classes and then compose a new class with them. Last comment regarding the three return functions, they are not really needed as you can access them after instantiation like so "megatron.o1.move()" - Idk if this is good python form or not - just saying they can be accessed like this.

  • @PTM1008

    @PTM1008

    Жыл бұрын

    megatron.o1.move() as you suggest doesn't sound right to me. What if o1 (dog) has a method called poop()? Being a machine, megratron won't be able to poop()... Hence, it is important for megatron to only expose the required functionality. It may not be a hard and fast rule. There might be scenarios where your suggestion will make more sense.

  • @d0opify
    @d0opify3 жыл бұрын

    how u handle states, owned by super but reflected in sub? ex varialbe for what to woof!

  • @banamali143
    @banamali143 Жыл бұрын

    Very nice👍

  • @hassansakr4993
    @hassansakr49935 жыл бұрын

    great video thanks a lot

  • @edinetgrunhed6000
    @edinetgrunhed60003 ай бұрын

    this is it folks!!

  • @rhkina
    @rhkina3 жыл бұрын

    Hi, Dave! Thank you for your video! It helped me a lot to study about inheritance and composition, but I understand the approach should be different. What about creating a class of actions and use them inside the objects as Matt suggests?

  • @davecrabbe4579

    @davecrabbe4579

    3 жыл бұрын

    I don't see a reply here from a Matt.. so not sure of the details

  • @moazelsawaf2000
    @moazelsawaf20004 жыл бұрын

    Thank you so much sir

  • @akio3524
    @akio35243 жыл бұрын

    thx so much!!!!!!!!!!!

  • @Kawlinz
    @KawlinzАй бұрын

    At around 12:00, in your class of Superbot, you directly reference three of their classes in the creation of the superbot... So I'm just learning and this is the part that I'm having trouble with. I thought a class wasn't supposed to reference other classes, but that makes it super difficult to do really simple things (in my example, I've made a nuclear power plant object, that when it is created it takes a certain amount of time to "power up" and then it creates a signal, which is a reference to another class, which is a no no). Is the proper way to use some sort of interface? Any advice is appreciated.

  • @PETERTRITSCH
    @PETERTRITSCH2 жыл бұрын

    great !!!

  • @eidiazcas
    @eidiazcas4 жыл бұрын

    I could have created a class called eater which contains that method eat, meower only with meow(), and barker with bark() only and make cat and robot cat inherit from those classes. How is composition different from multiple inheritance then?

  • @davecrabbe4579

    @davecrabbe4579

    4 жыл бұрын

    True, but that is not how classes get created. Classes, in OOP, generally all have lots of methods. The idea is to reuse methods of classes that you’ve already created without loading the entire class and inheriting all those classes you don’t need (and which could cause bugs).

  • @paragkadam2169
    @paragkadam21693 жыл бұрын

    Great video.. I noticed you did not show how you handled the duplication that was introduced by the eat() method after you converted your solution to composition, can you please tell me how to managed the duplicate eat() method in the dog and cat classes?

  • @davecrabbe4579

    @davecrabbe4579

    3 жыл бұрын

    I didn't show the eat() method for the dog/cat in the 2nd example, but one way to handle it is to create a new class called Animal with the eat() method. Here in animal you might put any method that could be used by multiple classes that you might want to create later. So you've only create the eat() method in one place. To add the eat() method to Superbot() you would add the lines: self.o4=Animal() and the method: def eat(self): return self.o4.eat()

  • @josephlyons3393
    @josephlyons33933 жыл бұрын

    Nice video. Just as a heads up, I don't think the return statements in the SuperBot class methods were needed.

  • @machinimaaquinix3178

    @machinimaaquinix3178

    Жыл бұрын

    Yes this puzzled me as well. Fwiw if anyones reading this past Sept 2022, the current version of python I tried this on (3.10.6) does not require all the function calls in Superbot and the methods can be accessed with this code: megatron = Superbot(): megatron.o1.move() megatron.o2.bark() megatron.o3.clean() megatron.play_games()

  • @prototype8861
    @prototype88613 жыл бұрын

    I want my 15 minutes back. He just literally stuffed an entire dog inside the robot so the bot can bark. Evil.

  • @user-hi9dd4dl9q
    @user-hi9dd4dl9q4 жыл бұрын

    Thanks

  • @milad5554
    @milad55544 жыл бұрын

    As i Understood, One of the benefits of using composition over inheritance is , from examples of this video , in inheritance when boss says "hey i need a SuperBot with Bark ability" , we can import whole Dog class into new SuperBot Class and its seems works ,but we are importing other abilities of Dog that we don't need them . so it may cause speed and memory problems . But in composition we just import the ability that we need. Is it true ?

  • @barsidemen5208

    @barsidemen5208

    4 жыл бұрын

    But here when we initialize the Dog object (self.01 = Dog()), aren't we importing all its method as well?

  • @milad5554

    @milad5554

    4 жыл бұрын

    @@barsidemen5208 I think no , because when we use composition we define objects by their abilities and we use abilities instead of whole object

  • @prashantbajpai2142

    @prashantbajpai2142

    4 жыл бұрын

    Yes...you are right @Milad Jafari, the possible solution to send Bark method from Dog class into SuperBot class as a method parameter. (Method as a function pointer with the help of delegates). I hope it makes sense.

  • @milad5554

    @milad5554

    4 жыл бұрын

    Prashant Bajpai 👍🙏

  • @freemanfreed1581
    @freemanfreed15813 жыл бұрын

    Subscribed !!! I need delegation in python

  • @ajaybhandari5180
    @ajaybhandari51806 ай бұрын

    I dont get it , isnt that violating the SOLID principle of DIP? why don't use abstractions for Dog, Cat, CleanRobot class and use these in a constructor of main class? correct me if I am wrong please.

  • @mr.anderson5393
    @mr.anderson53934 жыл бұрын

    Sir why did you use o1, o2, o3 with superRobot? Very good video sir.

  • @pianopat6924

    @pianopat6924

    3 жыл бұрын

    I think he used them to assign Dog, Robot and CleanRobot class to have access to their methods. superRobot uses these methods in defining its own move, bark, and clean methods. In short, so that he can make superRobot with these behaviors without redefining it's own move, bark, and clean methods. Even shorter - re-using code definitions in other objects without inheritance.

  • @Lavasea
    @Lavasea4 жыл бұрын

    I think a better implementation will have the cleaner robot class named cleaner, because all it does is clean, and it represents something that can clean. Then if you want to implement the original cleaner robot that can both clean and move, you can make a new class called CleanerRobot and have both a cleaner and a robot class in it.

  • @davecrabbe4579

    @davecrabbe4579

    Жыл бұрын

    yes.. this comment has come up before and it is a cleaner (excuse the pun) and more self-documenting method for the design.. the details are the same....

  • @JustAnotherAlchemist
    @JustAnotherAlchemist4 жыл бұрын

    Are you monologing some well known source? I ask, because your video is almost verbatim identical to Fun Fun Function's video on the same subject from three years prior.

  • @davecrabbe4579

    @davecrabbe4579

    4 жыл бұрын

    I did research from a bunch of different sites as there was not much information available on illustrating the concept of Composition.. Just watched the site you mentioned and no doubt I used much of that material for my in-class lecture.. I didn't realize I had used so much of it and I'll credit it in my description.

  • @houseofcodingwithnayan5937
    @houseofcodingwithnayan59372 жыл бұрын

    awesome explanation

  • @unabonger777
    @unabonger7775 жыл бұрын

    a pair abouts?

  • @davecrabbe4579

    @davecrabbe4579

    4 жыл бұрын

    a pair of Bots?

  • @rob876
    @rob8763 жыл бұрын

    Wouldn't you say that the recommendation to prefer composition over inheritance is the consequence of only having single inheritance and not multiple inheritance in some defective OO languages like Java and c#? With only single inheritance, it's better not to use the feature at all in that language - instead, roll your own multiple inheritance using composition.

  • @davecrabbe4579

    @davecrabbe4579

    3 жыл бұрын

    When you only use inheritance, you can often get a pile of things inherited that you didn’t plan. This can happen in any proper OOPL, such as Python. Using Composition only gets those methods that you need and keeps everything cleaner. Certainly Java had OOP tacked on and is more complex for many reasons.. such as the difference between primitives and object data types.. But composition is not something that is relative to the language. Even fundamentally object oriented languages will benefit from composition design where appropriate.

  • @rob876

    @rob876

    3 жыл бұрын

    @@davecrabbe4579 Thank You for your reply. I agree. I would only inherit from a class I was very sure of - such as one I'd recently written. If not, I'd wrap it and make sure not to reach into its member variables. On the other hand composition gives you so much more flexibility.