I've been told recently a good practice in object oriented programming that you should always allow inheritance from your classes. I really don't think so, but I have no solid arguments on my mind.
Real-world examples of blocked inheritance:
- No C++ STL class (specialized class template) allows inheritance (having non-virtual destructors).
- Java has its
final
class modifier that applies to many standard components, likejava.lang.String
.
Possible reasons I think are:
- Security, since subclass might have access to sensitive internals. (I don't think so -- they won't access private members.)
- Performance, since a subclass could mess up our efficient implementations by overriding some of the member functions. (Children won't override non-virtual functions.)
- To enforce composition over inheritance. (I fully agree. We shouldn't favor inheritance when it's not needed.)
So my question is: In what circumstances should I intentionally block inheritance?