I understand that one should use protected when one wants to make the variable visible in all classes that extend current class, but what does that mean exactly? What are the most common use cases?
I suppose another way to frame the question would be, what are the key cases when you would want a class variable to be visible from the class’ children but not from external classes.
Most of the use cases of the ‘protected’ access modifier I’ve encountered, are instantiations of the ‘Template Method’ pattern.
In this pattern, a detail of an algorithm is delegated to the subclass.
In effect, protected access creates a hole in your class invariants: a subclass may abuse the member in such a way that your invariant doesn’t hold anymore.
Most of the time, there are better design alternatives than protected access, dependency inversion being the first one coming to mind.
My advise? Don’t publish your object’s internals to anyone you don’t trust. Use ‘protected’ with caution!