There are many ways in which changing a base class can affect the behavior of a derived class. What if the author suddenly decided to make a sealed class for example?
Like any interface, it should be stable if consumers do not need modification.
The main βruleβ with inheritance is the Liskov Signature Principle. This means that the derived class must be replaced for the base class or other classes derived from it.
This case violates this rule on his face, because the rectangle with the hole is not a rectangle.
It is usually best to use interfaces to divide behavior into reasonable implementable chunks. Such interfaces are usually called adjectives, not nouns. For example, this makes sense for both a rectangle and a rectangle with a hole for display, so the interface can be IRenderable . If it is changed, you may have IResizable , etc. They can be combined in IShape , but you want to be careful that your Shape definition only defines the behavior of your problem domain.
Deriving directly from another class can be dangerous, since you are then bound by the behavior of that class. If you really need to do this, it is best to extract the required implementation into a common base class (e.g. Rectangle : RectangleImplementation, IShape ).
mancaus
source share