Should all inherited attributes be used?

Asked

Viewed 67 times

2

I am constructing a class diagram, where several classes have 3 equal attributes, and one or another class has only 2 of these attributes. So I thought I’d create a parent class with all these repeating attributes, and raise several daughters.

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

  • 1

    Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site.

1 answer

4

This is incorrect, because it certainly violates the the principle of Liskov’s substitution.

In fact it seems that 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 it is to eliminate repetition you must use composition and not inheritance. Inheritance should only be used when there is a relation of be a, that is to say, the derived class must be the same as the base class with something else. It must have nothing less, it must have nothing else. Inheritance at most eliminates repetition as a side effect.

Even the composition is to wonder if it should do so, probably should make a interface segregation, including because you are probably putting together unrelated things in fact.

This is ideal. The pragmatic knows when to violate everything and get more benefits than harm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.