Inheritance versus composition
Inheritance is a very important concept and is, without a doubt, a great way to recycle code under the right circumstances. However, many new or inexperienced programmers will often use inheritance as a means of importing code. This is a bad practice because instead of producing clean organized code, they produce jumbled-up code that has no true relationship between function blocks. When developing object-oriented code, it is very important to consider the relationships between function blocks. One very common way to implement object-oriented relationships is with a concept known as composition.
When to use composition
For many inexperienced, traditional programmers, composition is often an ill-understood but, ironically, often-used concept. Composition is where you include object references from one function block in another. In other words, composition allows us to assemble things. Whereas inheritance consists of an is a relationship between functions...