Public versus private inheritance when some of the parent's methods need to be exposed?
Posted
by
Vorac
on Programmers
See other posts from Programmers
or by Vorac
Published on 2014-06-13T13:36:08Z
Indexed on
2014/06/13
15:39 UTC
Read the original article
Hit count: 251
inheritance
|encapsulation
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).
© Programmers or respective owner