Should all inherited attributes be used?

2

I am building a class diagram, where several classes have 3 equal attributes, and one class has only 2 of these attributes. So I thought about creating a parent class with all those repeating attributes, and creating multiple daughters.

In these few classes that do not use the 3 attributes that are repeated in most other classes, are there problems in not using 2 of these attributes? Or is this incorrect?

    
asked by anonymous 19.03.2017 / 03:19

1 answer

4

This is incorrect because it certainly violates the Liskov replacement principle .

In fact, it seems that you did not understand what the inheritance is for. It is not to eliminate repetitions, at most it is to have a DRY , which is different from not repeating.

First, if you want to eliminate repetition, you must use composition and not inheritance. Inheritance should only be used when there is being a relationship, that is, the derived class must be the same thing as the base class with something else. It should not have anything less, it should not have anything different . Inheritance at most eliminates repetition as a side effect.

Even if the composition is to think about doing it this way, you should probably do a interface segregation , including because he's probably putting things together unrelated.

This is ideal. The pragmatist knows when to break everything and get more benefits than harm.

    
19.03.2017 / 11:29