Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should you ever use protected member variables?

Tags:

oop

protected

Should you ever use protected member variables? What are the the advantages and what issues can this cause?

like image 200
John Channing Avatar asked Aug 31 '08 18:08

John Channing


People also ask

Why protected variables should be avoided?

Protected variables should be avoided because: They tend to lead to YAGNI issues. Unless you have a descendant class that actually does stuff with the protected member, make it private. They tend to lead to LSP issues.

What is the point of protected variables?

Protected variables are those data members of a class that can be accessed within the class and the classes derived from that class. In Python, there is no existence of “Public” instance variables. However, we use underscore '_' symbol to determine the access control of a data member in a class.

When should I use protected?

- When a class inherits a base class, all the data members except the private get inherited into it. So if we want data members to be accessible to only derived classes and not privately or publicly accessible, then we can use protected. - Protected is similar to private.

Are protected methods bad?

First of all, having only “protected” members in a class breaks encapsulation from the perspectives of a parent class. It allows users to change the state and the behavior inherited from the parent class even thought the author of the parent class didn't plan that.


2 Answers

Should you ever use protected member variables?

Depends on how picky you are about hiding state.

  • If you don't want any leaking of internal state, then declaring all your member variables private is the way to go.
  • If you don't really care that subclasses can access internal state, then protected is good enough.

If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't see the implementation specific details of how things are being done which gives you the flexibility of changing it later.

like image 95
Allain Lalonde Avatar answered Oct 14 '22 05:10

Allain Lalonde


Generally, if something is not deliberately conceived as public, I make it private.

If a situation arises where I need access to that private variable or method from a derived class, I change it from private to protected.

This hardly ever happens - I'm really not a fan at all of inheritance, as it isn't a particularly good way to model most situations. At any rate, carry on, no worries.

I'd say this is fine (and probably the best way to go about it) for the majority of developers.

The simple fact of the matter is, if some other developer comes along a year later and decides they need access to your private member variable, they are simply going to edit the code, change it to protected, and carry on with their business.

The only real exceptions to this are if you're in the business of shipping binary dll's in black-box form to third parties. This consists basically of Microsoft, those 'Custom DataGrid Control' vendors, and maybe a few other large apps that ship with extensibility libraries. Unless you're in that category, it's not worth expending the time/effort to worry about this kind of thing.

like image 33
Orion Edwards Avatar answered Oct 14 '22 04:10

Orion Edwards