Why is public inheritance advocated when reducing the publicly visible API seems preferable from a maintainability perspective?

StackOverflow https://stackoverflow.com/questions/8952287

Вопрос

I am using bada and refer to the tutorial here, which begins:

    class MainForm:
       public Osp::Ui::Controls::Form,
       public Osp::Ui::IActionEventListener,
       public Osp::Ui::ITouchEventListener
    {

I am running code where I recently removed the public specifier to cut down on my public API. You'll see that the functions implementing those interfaces where all also declared publicly, for which I saw no need and made private. I would do this without hesitation when implementing my own interfaces when those interfaces may provide more access than I would wish regular clients of my concrete class to receive.

What is the reason for making them public, what am I missing?

I guess it is advocated to aid extensibility, but for a dev making apps not libraries I would challenge this wisdom.

Это было полезно?

Решение

If Form, IActionEventListener and ITouchEventListener already support many usable methods, in most cases why hide them? On the contrary: if you hide them and in the future someone will need them, it will be harder for you to maintain the class because you'll need to provide them again.

If you need to hide the parent's methods, there's another way to do this: instead of inheriting, enclose the "parent" as a field in your new class.

In some languages such as C#, public inheritance is the only option.

Другие советы

For me private inheritance of "interfaces" is a non sens.

The interface of an object is its set of public methods. As llya said, if you want to use the functionalities provided by a class internally, use object composition. If you want to provide a subset of the interface, then either compose or simply declare a more restrictive interface.

If the "interface" and the functions taking object from this interface are in a third party library then its means that the developers wanted to force you to implement every methods, so you have to provide them.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top