Is inheritance of specific classes evil?

I use interfaces / abstract base classes for most of my types and do not often inherit from specific classes, but I recently encountered a situation where inheritance or composition is desired. I knew about the "proverb" program to an interface, not to an implementation, "but recently decided to go deeper.

I saw the arguments against inheritance and I saw the counter arguments , but I'm curious about what other supporting large codebases actually do. Is fear bloated? Do you inherit from specific classes or are skeptics of inheritance correct? I am particularly interested in hearing from those people who work in C ++.

+10
c ++ design
Oct 08 '10 at 5:12
source share
3 answers

Not a C ++ guy (you have professional experience working with large corporate systems in C #, java and ruby), but here are my 2 cents anyway

It is not black and white.

The problem with inheritance is that it introduces a tight connection. Worse, this connection usually occurs around the encapsulated state. Changes in superclasses can affect hierarchical inheritance hierarchies, creating subtle and difficult to predict errors.

Interface segregation completely wraps around these issues because it does not break encapsulation in the same way.

The good thing about inheritance is that sometimes you have an object model that is actually the same, just with very little change or addition. As long as this change is very clear, not covered by wide scope and is not an additional restriction (see circle-ellipse problem), code reuse will exceed a tight connection.

I stick to composition over inheritance as a rule, not the law.

+11
Oct 08 '10 at 5:31
source share

You can privately get specific classes. Private inheritance does not require replacement of Liskov; this is essentially just a convenient way to “mix” class functionality.

Our inheritance, on the other hand, requires the replacement of Liskov. Thus, usually public inheritance from specific classes is bad, unless such a class is intended to be used as a base class (many of them are not).

+4
Oct 08 '10 at 5:15
source share

A good design requires specific classes to do one relatively small thing well and efficiently. In particular, in C ++, specific classes emphasize their similarity with specific types, such as int and char , often during operator overloading and having only non-virtual functions. Because specific classes are not intended to display polymorphic behavior, they should not be inherited. Also see Section 25.2 C ++ Programming Language for more information on the proper use of specific types in this language.

+2
Oct 08 2018-10-10 at
source share



All Articles