I heard that it is not good to use protected members, because it breaks the encapsulation of base class. I need to know what are the advantages of using protected members over private members or vice-verse in base classes. Can anyone help?
Share
There is no clear advantage of using protected members over using private members. It is conceptually different in any way.
private: You can only use your member inside the class
protected: Member can be used inside the class and inside it’s subclasses, but not from outside
public: Member can be called and seen anywhere inside and outside the class
This means: Choose your concept based on what you want to express! If you have to override members, make them protected.
What is definitly not recommend is to make members protected for the “just in case” situation. If you want to express that there is a hook which can be overriden, then and only then, make them protected. This has to be expressed in your code elsewhere, e.g. where the overriden member is used.