Public versus private inheritance when some of the parent's methods need to be exposed?
- by Vorac
Public inheritance means that all fields from the base class retain their declared visibility, while private means that they are forced to 'private' within the derived class's scope.
What should be done if some of the parent's members (say, methods) need to be publicly exposed?
I can think of two solution. Public inheritance somewhat breaks encapsulation. Furthermore, when you need to find out where is the method foo() defined, one needs to look at a chain of base classes.
Private inheritance solves these problems, but introduces burden to write wrappers (more text). Which might be a good thing in the line of verbosity, but makes changes of interfaces incredibly cumbersome.
What considerations am I missing? What constraints on the type of project are important? How to choose between the two (I am not even mentioning 'protected')?
Note that I am targeting non-virtual methods. There isn't such a discussion for virtual methods (or is there).