Why should we prefer composition over inheritance?

Asked

Viewed 75 times

5

Since the popularization of object orientation in the 90’s I hear that composition should be preferred before creating hierarchy of types. But perhaps the most important concept of object orientation is heritage.

They say you can only use inheritance when the guy is a type he inherits, that there may be a direct substitution of one for the other (the principle of Liskov’s replacement). And the composition should be used when the one type has a another kind.

Why is this necessary or more advantageous? Gives the impression that it is an object orientation failure.

It is then possible to use only the composition without major disadvantages?

Is there any way to see clearly when inheritance is better?

  • I’m sorry for the flaw, there was already something that answers the essence of what I wanted.

No answers

Browser other questions tagged

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