Member-only story
Contract and Inheritance
Inheritance Is Bad, Isn’t it?
Well, the section topic might be misleading and causes some controversies. In the modern object-oriented design, we tend to pursue loosely-coupling implementations as possible as we can, which seems like a trend. And more and more programmers advocate not to use inheritance because it often results in hard-to-maintain modules as well as unnecessary dependencies.
However, is it so terrible as we have seen on the appearance? I don’t think so. If you are favorable with learning how to craft a well-maintained architecture. Might you see some sentences as below somewhere.
Favor object composition over class inheritance
or something like this …
Program to an interface, not an implementation
Both are cited from the well-known design references but are easy to be incorrectly-explained because it is possible for people to implicitly consider inheritance as a bad coding habit. However, it is not a truth. Most painfulness results from misuse of inheritance so that many developers start referring to it as an origin of the chaos.
How to avoid an improper inheritance, and even refine the design ASAP?
The question came into my mind when I started to learn “Design by Contract”.