Some languages with single inheritance like PHP, developers are forced to use inheritance to solve most of the problems that aim to reduce redundancy in code. What this does in the long run is, developers end up forming relationships between otherwise unrelated classes, by using super classes, abstract classes and interfaces, to define a common behavior/interface between these two unrelated classes.  For instance if you have toothpaste, and a fire extinguisher, you may end up having two classes, Toothpaste and Extinguisher, which are very unrelated. However, they both have a date of manufacture and a date of expiry. To get the lifetime of these two items, you will need the two dates, do the math, and get how long each item is meant to last.  One solution to this problem would be to use an interface or an abstract class, or even a normal class, with a method that does the math, and have the two classes inherit the class. This is the kind of relationships am talking about. Traits are meant to prevent such relationships from happening, and let relationships happen where necessary, like the old humble ‘Animal, Dog, Cat and Cow’ analogy.

– See more at: Classmite

Advertisements