Wednesday, January 1, 2014

Should you ever use protected member variables? What are the the advantages and what issues can this cause?
share|improve this question
add comment

10 Answers

up vote41down voteaccepted
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.
share|improve this answer
Can you comment on performance of protected variables vs a private variable with an get/set method? –  JakeJun 24 '10 at 13:56
I'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
@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. –  MeckiNov 25 '10 at 17:35 
add comment
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.
share|improve this answer
add comment
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.
share|improve this answer
add comment


oakleyses said...

louis vuitton handbags, oakley sunglasses, louboutin, longchamp outlet, nike shoes, louis vuitton outlet stores, chanel handbags, burberry outlet, prada outlet, jordan shoes, tiffany and co, michael kors outlet, tory burch outlet, louis vuitton outlet, longchamp handbags, nike free, true religion jeans, michael kors outlet, kate spade outlet, polo ralph lauren outlet, tiffany and co, prada handbags, polo ralph lauren outlet, michael kors outlet, michael kors outlet, longchamp handbags, oakley sunglasses, ray ban sunglasses, kate spade handbags, burberry outlet, louis vuitton outlet, louboutin outlet, louboutin, coach factory outlet, air max, air max, coach outlet, gucci outlet, christian louboutin shoes, michael kors outlet, coach purses, ray ban sunglasses, michael kors outlet, louis vuitton, coach outlet store online, true religion jeans, oakley sunglasses cheap

oakleyses said...

ralph lauren, lululemon, air max, hollister, north face, nike air max, polo lacoste, vanessa bruno, timberland, vans pas cher, louboutin, louis vuitton, oakley pas cher, air max pas cher, nike roshe run, air max, true religion outlet, barbour, sac longchamp, air force, hollister, sac louis vuitton, nike free, polo ralph lauren, nike trainers, louis vuitton uk, nike roshe, sac hermes, longchamp, michael kors, sac burberry, sac guess, mulberry, new balance pas cher, converse pas cher, sac louis vuitton, hogan outlet, nike tn, north face, true religion outlet, ray ban pas cher, michael kors, air jordan, nike blazer, nike free pas cher, michael kors pas cher, abercrombie and fitch, ray ban sunglasses

oakleyses said...

mac cosmetics, mont blanc, marc jacobs, canada goose outlet, nike huarache, vans shoes, soccer jerseys, hollister, giuseppe zanotti, beats by dre, abercrombie and fitch, longchamp, insanity workout, celine handbags, bottega veneta, ghd, nfl jerseys, north face outlet, chi flat iron, ugg boots, birkin bag, ugg australia, canada goose, herve leger, ugg pas cher, rolex watches, valentino shoes, canada goose uk, canada goose, ferragamo shoes, canada goose, ugg boots, uggs outlet, north face jackets, soccer shoes, asics running shoes, new balance shoes, p90x, lululemon outlet, canada goose jackets, mcm handbags, instyler, babyliss pro, ugg, wedding dresses, jimmy choo outlet, reebok outlet, nike roshe run

oakleyses said...

parajumpers, karen millen, air max, converse, pandora charms, moncler, louboutin, moncler, links of london, lancel, juicy couture outlet, oakley, hollister, pandora charms, supra shoes, thomas sabo, canada goose, gucci, wedding dresses, timberland boots, swarovski crystal, air max, coach outlet store online, moncler, ray ban, canada goose, moncler, ugg, louis vuitton, swarovski, hollister, montre homme, moncler, hollister clothing store, ralph lauren, rolex watches, moncler outlet, moncler, iphone 6 cases, baseball bats, juicy couture outlet, toms shoes, vans, pandora jewelry, ugg, converse shoes

Zheng junxai5 said...

juicy couture
supra for sale
vans sneakers
timberland boots
fitflop shoes
burberry outlet
louboutin shoes
fitflop clearance
nike trainers sale
polo ralph lauren outlet
ralph lauren
fitflop shoes
longchamp outlet
toms shoes outlet online
jordan 11
replica rolex watches
coach outlet
ray ban sunglasses
michael kors outlet clearance
coach outlet
giuseppe zanotti outlet
michael kors outlet clearance
jordan shoes
cheap air jordans
michael kors outlet online
oakley vault
ralph lauren outlet
christian louboutin shoes
air force 1 trainers
ed hardy outlet
retro jordans 13
gucci outlet online
air jordan 8
louis vuitton bags
louis vuitton handbags
asics running shoes
replica rolex watches
louis vuitton outlet
coach outlet
coach factory outlet

raybanoutlet001 said...

cheap air jordan
michael kors handbags
adidas stan smith
michael kors uk
adidas stan smith men
fitflops sale
cheap nfl jerseys
christian louboutin shoes
nike air huarache
discount oakley sunglasses
huarache shoes
cheap basketball shoes
nike zoom
oakley sunglasses
tiffany online
jordan shoes on sale
adidas nmd for sale
nike air zoom structure 19
yeezy shoes
air jordans,cheap air jordans,air jordan shoes,air jordan 11,air jordan 13,air jordan 6,air jordan 4