Guru on Rails
What are the reasons for using Inheritance and Composition. Speaking of Inheritance, we refer to class, for Composition we refer to Module. We should know the main idea from this design. Why do we have those kinds of concepts? That is decoupling code.
Looking at the application, design is nothing except so many messages, but the most organizational structure is the class. If we look at an application, we can see at least a well-design should construct the class as the unit. We call it Object Oriented Design.
Each class will have the specific knowledge about other classes. That's when we involve an instance's function of a class. It means a message is sent in this context. We call the knowledge about a class is a dependency. Hence, it's easy to see that if these classes have too much knowledges of the others, it means each one has a ton of dependencies - connections. That makes it difficult for devs to maintain and evolve the system. Because every time we make changes on a class, it affects so many other classes. I believe that coding more, getting more bugs.
A well-design will embrace the changes, making the less cost for changes. Hence, we try to decouple classes, modules, methods. Inheritance and Composition appear to solve this matter. Now, when we design a new class, if we can, separating the code we think that it will be changed regularly from another which is certain, less change. So, we recognised them, where do we put the changeable code? The less changeable code should be at the parent - Inheritance. If we think that several methods don't actually depend on the instance of class and they can be re-used for other instance or other class as well, we can put them to modules to share code. We will get DRY in design pattern.
That's it. Inheritance and Composition will help us to decouple code, making well-design. The difference is the concept. Both of them do the same thing, moving or extracting changeable code from unchangeable one. We don't anticipate exact what will be changed in the future though. However, we can code for both today and tomorrow. The code today embraces the changes in the future. Inheritance and Composition provide us the convenient ways to do it.