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
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!
One of the most Simple and Best explanations on this topic!!
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.
Have actually watched about 10 videos for composition vs inheritance this is still the best
@davecrabbe4579
Жыл бұрын
thx
Finaly some good explanation what is composition! Other videos were about rooms, walls, furniture and windows...
I am having difficulties understanding this topic, but this explained far way better than the other. Great job.
@davecrabbe4579
2 жыл бұрын
This was a topic that gave most of my students difficulty.. so you are not alone.
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.
Provided a quick reference with some simple and easy to understand examples. Thanks a ton, helped me with my homework
You are amazing! The best video on this subject I have seen! Thank you!
Wow I hope you make more Python and Object Oriented Design videos! This is top-notch!
Very good explanation, tried to read from many sites but this 15mn video is all you need.
One of the clear explanations on composition..
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.
I liked this a lot. Super clear, well explained.
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.
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
4 жыл бұрын
nice 👍
@Catalin52
2 жыл бұрын
This is the right approach 👍
@davecrabbe4579
Жыл бұрын
yes.. I would tend to agree with this.. If starting from scratch this is a better way to proceed. Good thinking.
@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.
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
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!
I will be back here later, thank you for great explanation once again
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
3 жыл бұрын
can you give an example?
@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
3 жыл бұрын
@@MichaelLamTheDeveloper Hey, can you please provide an example for what you'd consider a good refactor of the video?
@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
3 жыл бұрын
@@yonishachar1887 interfaces. ICanBark, ICanMove etc.
Good video. Had to watch it at x1.5, though :)
@aMulliganStew
4 жыл бұрын
2x but that didn't bother me. I listen far more quickly than I speak.
You are the best teacher!
Amazing and simple!
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
5 жыл бұрын
thank you for the comments
finally got the video that can explain the difference between inheritance and composition clearly , great job Mr. Dave
Totally clear!! Thanks
Thanks for this awesome video. Good stuff!
Great VIdeo..How easily you explained it is awesome..Great work and Thanks for video
Thanks a lot, the best video of this topic.
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".
Made the concept clear. Keep it up.
Excellently described!!!
Well and simple described , Thank you
Thanks for explanation!
Really helped me. Thanks a lot!
Amazing explanation!
Thank you so much Dave!!!
Thank you Soooo much!! Best explanation!!! 💙👍
thank you so much, this explanation is very clear
Thank you! Amazing content
"We need a robot that can clean, move, bark + play games..." 😂😂 That's what a boss would say
wow, i am the 10000th viewer. Great video, really helps, thank you.
great explanation!
very gooooooddddddd you are very clear
Brilliant.......
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
Жыл бұрын
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.
how u handle states, owned by super but reflected in sub? ex varialbe for what to woof!
Very nice👍
great video thanks a lot
this is it folks!!
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
3 жыл бұрын
I don't see a reply here from a Matt.. so not sure of the details
Thank you so much sir
thx so much!!!!!!!!!!!
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.
great !!!
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
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).
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
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()
Nice video. Just as a heads up, I don't think the return statements in the SuperBot class methods were needed.
@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()
I want my 15 minutes back. He just literally stuffed an entire dog inside the robot so the bot can bark. Evil.
Thanks
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
4 жыл бұрын
But here when we initialize the Dog object (self.01 = Dog()), aren't we importing all its method as well?
@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
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
4 жыл бұрын
Prashant Bajpai 👍🙏
Subscribed !!! I need delegation in python
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.
Sir why did you use o1, o2, o3 with superRobot? Very good video sir.
@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.
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
Жыл бұрын
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....
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
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.
awesome explanation
a pair abouts?
@davecrabbe4579
4 жыл бұрын
a pair of Bots?
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
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
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.