As a software developer, translating ideas into code is a day-to-day job. The object-oriented programming (OOP) paradigm is widely used. Its strengths include abstraction, encapsulation and polymorphism, to name a few. OOP loses its flexibility if implementing the code does not account for limitations. Although inheritance looks straight forward when we want to reuse code or implement polymorphism, it may end up being difficult to maintain code.
Let's look at an example. If we have to implement an assignment entity, we know all assignments have common properties like name, due date and time, scores and so on. In our LMS (NexPort), both course assignments (providing content to read) and test assignments can be launched by a student without an instructor's involvement. So, we add a method or property to launch assignments in the base class. Now we have to implement writing assignments or discussion assignments that have to be moderated by an instructor. These assignments will inherit methods or properties related to the launching behavior but this is functionality that they DO NOT support. Further compounding our problem is the instructor role, should this be supported in the base class? In such a scenario, the code becomes unnecessarily coupled.
This question remains: how should inheritance be implemented? First, inheritance hierarchy is always an "is-a" relationship and not a "has-a" relationship. Whenever we have a "has-a" relationship, it should be composition of objects not inheritance. Secondly, inheritance hierarchy should be reasonably shallow and the developer has to make sure other developers are not likely to add more levels.
About NexPort Solutions Group
NexPort Solutions Group is a division of Darwin Global, LLC, a systems and software engineering company that provides innovative, cost-effective training solutions and support for federal, state and local government, as well as the private sector.