This article is about creating a data model that would nicely fit SQL and contain “proper” OOP inheritance. I must say that I encountered this problem at various times on different projects, and I solved it in different ways. The names of the approaches are taken from the terminology that has developed on the relevant projects.
The concept of good or bad design is relative. At the same time, there are some programming standards, which in most cases guarantee effectiveness, maintainability, and testability. For example, in object-oriented languages, this is the use of encapsulation, inheritance, and polymorphism. There is a set of design patterns that in a number of cases have a positive or negative effect on the application design depending on the situation. On the other hand, there are opposites, following which sometimes leads to the problem design.
In this article, I will try to describe one of the famous principles of the object oriented programming, that is a part of another famous term – SOLID. This principle is called Single Responsibility, and it states that:
A class should have only one reason to change. (more…)
In the object-oriented programming world, the concept of inheritance has been criticized for a long time.
There are quite a lot of arguments:
- A derived class inherits all the parent’s data and behavior that is not always necessary. When modifying a parent class, some data and behavior that are not supposed to be in the derived class get into it;
- Virtual methods are less productive. If a language allows declaring a non-virtual method, what would you do when you need to override one in an inherited method? You can mark the method with the new keyword, however, in this case, polymorphism will not work. The consequent use of this object may cause unexpected behavior, depending on the type the object is going to be converted to.
- The vast majority of languages do not allow multiple inheritance.
- There are tasks where inheritance is helpless. Consider the case, when you need to create a container (array, list, or set) and implement the same behavior for elements with different types. In addition, you need to provide static typing. In this case, generics will come to help.