Should you ever use protected member variables? What are the the advantages and what issues can this cause?
10 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.

- 91,574
- 70
- 187
- 238
-
1Can you comment on performance of protected variables vs a private variable with an get/set method? – Jake Jun 24 '10 at 13:56
-
3I'd say it's not something worth worrying about unless you find through profiling that the bottle neck ends up being the accessors (which it almost never is). There are tricks that can be done to make the JIT smarter about things if it ends up being an issue. In java for example you can hint that the accessor can be inlined by marking it as final. Though honestly, the performance of getters and setters is far less important than dealing with system organization and with the actual performance problems as determined by a profiler. – Allain Lalonde Jun 25 '10 at 18:07
-
25@Jake: You should never make design decisions based on performance assumptions. You make design decisions based upon what you think is the best design and only if you real life profiling shows a bottleneck in your design, you go and fix it. Usually if the design is sound, the performance is good as well. – Mecki Nov 25 '10 at 17:35
-
**With private members, other than the public interface, they can't see the implementation specific details** They can just open the class and look it up, so that makes no sense?! – Black Mar 04 '17 at 15:49
-
4@Black Clearly Allain meant 'they can't **access**' those members and therefore can't build code against them, leaving the author of the class free to remove/change the protected members later. (Of course, the pimpl idiom would enable hiding them visually and from the translation units including the header, too.) – underscore_d May 12 '17 at 20:57
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.

- 121,657
- 64
- 239
- 328
-
1I think this is the pragmatic answer. I came to the same conclusion myself – uriDium Feb 21 '22 at 06:59
-
1
-
@TomMcLean Not really, no. It's a fine tool for a narrow problem space, but I think people overuse it for a lot of other scenarios where it hurts more than it helps. – Orion Edwards Nov 06 '22 at 00:59
-
@OrionEdwards In a favor composition over inheritance way or functional programming is better than object-oriented programming? – Tom McLean Nov 06 '22 at 21:14
-
1@TomMcLean OO, FP, Composition and Inheritance are all styles of program design that have various strengths and weaknesses. One is not "better" than another, but rather each works best in different situations. My advice would be to try gain experience with each so you can work out what to employ for the problems you are working on at the time. – Orion Edwards Nov 10 '22 at 20:07
The general feeling nowadays is that they cause undue coupling between derived classes and their bases.
They have no particular advantage over protected methods/properties (once upon a time they might have a slight performance advantage), and they were also used more in an era when very deep inheritance was in fashion, which it isn't at the moment.

- 39,055
- 11
- 90
- 118
-
2Shouldn't `no particular advantage over protected methods/properties` be `no particular advantage over *private* methods/properties`? – Penghe Geng Sep 25 '14 at 14:12
-
No, because I am/was talking about the advantages/disadvantages of various ways of communicating between derived classes and their bases - all these techniques would be 'protected' - the difference is whether they are member variables (fields) or properties/methods (i.e subroutines of some kind). – Will Dean Sep 25 '14 at 15:19
-
1Thanks for the quick clarification. I am glad to have the original poster's reply in an hour for my question to a 6 year old post. You don't think that can happen in most other online forums :) – Penghe Geng Sep 26 '14 at 00:05
-
11More remarkable still is that I actually agree with myself across that amount of time... – Will Dean Sep 26 '14 at 07:35
-
One order of business of a constructor is to see to it that all state variables are explicitly initialized. If you adhere to this convention, you can use the `super` construct to call the parent constructor; it will then take care of initializing private state variables in the parent class. – ncmathsadist Dec 09 '14 at 14:22
The key issue for me is that once you make a variable protected, you then cannot allow any method in your class to rely on its value being within a range, because a subclass can always place it out of range.
For example, if I have a class that defines width and height of a renderable object, and I make those variables protected, I then can make no assumptions over (for example), aspect ratio.
Critically, I can never make those assumptions at any point from the moment that code's released as a library, since even if I update my setters to maintain aspect ratio, I have no guarantee that the variables are being set via the setters or accessed via the getters in existing code.
Nor can any subclass of my class choose to make that guarantee, as they can't enforce the variables values either, even if that's the entire point of their subclass.
As an example:
- I have a rectangle class with width and height being stored as protected variables.
- An obvious sub-class (within my context) is a "DisplayedRectangle" class, where the only difference is that I restrict the widths and heights to valid values for a graphical display.
- But that's impossible now, since my DisplayedRectangle class cannot truly constrain those values, as any subclass of it could override the values directly, while still being treated as a DisplayedRectangle.
By constraining the variables to be private, I can then enforce the behaviour I want through setters or getters.

- 2,679
- 5
- 32
- 60
In general, I would keep your protected member variables to the rare case where you have total control over the code that uses them as well. If you are creating a public API, I'd say never. Below, we'll refer to the member variable as a "property" of the object.
Here's what your superclass cannot do after making a member variable protected rather than private-with-accessors:
lazily create a value on the fly when the property is being read. If you add a protected getter method, you can lazily create the value and pass it back.
know when the property been modified or deleted. This can introduce bugs when the superclass makes assumptions about the state of that variable. Making a protected setter method for the variable keeps that control.
Set a breakpoint or add debug output when the variable is read or written to.
Rename that member variable without searching through all the code that might use it.
In general, I think it'd be the rare case that I'd recommend making a protected member variable. You are better off spending a few minutes exposing the property through getters/setters than hours later tracking down a bug in some other code that modified the protected variable. Not only that, but you are insured against adding future functionality (such as lazy loading) without breaking dependent code. It's harder to do it later than to do it now.

- 4,240
- 3
- 37
- 41
At the design level it might be appropriate to use a protected property, but for implementation I see no advantage in mapping this to a protected member variable rather than accessor/mutator methods.
Protected member variables have significant disadvantages because they effectively allow client code (the sub-class) access to the internal state of the base class class. This prevents the base class from effectively maintaining its invariants.
For the same reason, protected member variables also make writing safe multi-threaded code significantly more difficult unless guaranteed constant or confined to a single thread.
Accessor/mutator methods offer considerably more API stability and implementation flexibility under maintenance.
Also, if you're an OO purist, objects collaborate/communicate by sending messages, not reading/setting state.
In return they offer very few advantages. I wouldn't necessarily remove them from somebody else's code, but I don't use them myself.

- 7,499
- 3
- 32
- 50
Just for the record, under Item 24 of "Exceptional C++", in one of the footnotes, Sutter goes "you would never write a class that has a public or protected member variable. right? (Regardless of the poor example set by some libraries.)"

- 1,118
- 3
- 16
- 30
Most of the time, it is dangerous to use protected because you break somewhat the encapsulation of your class, which could well be broken down by a poorly designed derived class.
But I have one good example: Let's say you can some kind of generic container. It has an internal implementation, and internal accessors. But you need to offer at least 3 public access to its data: map, hash_map, vector-like. Then you have something like:
template <typename T, typename TContainer>
class Base
{
// etc.
protected
TContainer container ;
}
template <typename Key, typename T>
class DerivedMap : public Base<T, std::map<Key, T> > { /* etc. */ }
template <typename Key, typename T>
class DerivedHashMap : public Base<T, std::hash_map<Key, T> > { /* etc. */ }
template <typename T>
class DerivedVector : public Base<T, std::vector<T> > { /* etc. */ }
I used this kind of code less than a month ago (so the code is from memory). After some thinking, I believe that while the generic Base container should be an abstract class, even if it can live quite well, because using directly Base would be such a pain it should be forbidden.
Summary Thus, you have protected data used by the derived class. Still, we must take int o account the fact the Base class should be abstract.

- 81,378
- 38
- 130
- 159
-
it doesn't break encapsulation anymore than public members do. Its a setting to say that derived classes can use the class' state that isn't exposed to users of the class. – gbjbaanb Nov 15 '08 at 01:42
-
@gbjbaanb: You are contradicting yourself "it doesn't break encapsulation anymore than public members do" is different from "[only] derived classes can use the class' state". "protected" is the middle between public and private. So "protected [...] breaks somewhat the encapsulation" is still true... – paercebal Nov 30 '08 at 20:30
-
actually, in the c++ language, container adapters like std::stack will expose the underlying container object with a protected variable called "c". – Johannes Schaub - litb Dec 10 '08 at 21:57
-
I know this post is quite old, but i feel the need to chime in. You don't "somewhat" break encapsulation, you completely break it. `protected` is no more encapsulated than `public`. I'm willing to be proven wrong. All you have to do is write a class with a protected member and prohibit me from modifying it. Obviously the class has to be non-final, since the whole point of using protected is for inheritance. Either something is encapsulated, or it isn't. There is no in-between state. – Taekahn Jul 07 '20 at 15:01
In short, yes.
Protected member variables allow access to the variable from any sub-classes as well as any classes in the same package. This can be highly useful, especially for read-only data. I don't believe that they are ever necessary however, because any use of a protected member variable can be replicated using a private member variable and a couple of getters and setters.

- 3,241
- 4
- 28
- 25
-
2Conversely, private member variables are also never needed; public is sufficient for any usage. – Alice Oct 20 '14 at 02:54
For detailed info on .Net access modifiers go here
There are no real advantages or disadvantages to protected member variables, it's a question of what you need in your specific situation. In general it is accepted practice to declare member variables as private and enable outside access through properties. Also, some tools (e.g. some O/R mappers) expect object data to be represented by properties and do not recognize public or protected member variables. But if you know that you want your subclasses (and ONLY your subclasses) to access a certain variable there is no reason not to declare it as protected.
-
1Wanting subclasses to _access_ a variable is very different from wanting them to be able to freely _mutate_ it. That's one of the main arguments against protected variables: now your base class can't assume any of its invariants hold, because any derived class can do absolutely anything with the protected members. That's the main argument against them. If they just need to _access_ the data, then... write an accessor. :P (I do use protected variables, though probably more than I should, and I'll be trying to cut-down!) – underscore_d May 12 '17 at 21:07