One of the first things I do when I subclass a class is to change a
bunch of private methods to protected
Some reasoning about private vs. protected methods:
private methods prevent code reuse. A subclass cannot use the code in the private method and may have to implement it again - or re-implement the method(s) which originally depend on the private method &c.
On the other hand, any method which is not private can be seen as an API provided by the class to "the outer world", in the sense that third-party subclasses are considered "outer world" too, as someone else suggested in his answer already.
Is that a bad thing? - I don't think so.
Of course, a (pseudo-)public API locks the original programmer up and hinders refactoring of those interfaces. But seen the other way around, why should a programmer not design his own "implementation details" in a way that's as clean and stable as his public API? Should he use private so that he can be sloppy about structuring his "private" code? Thinking maybe that he could clean it up later, because no one will notice? - No.
The programmer should put a little thought into his "private" code too, to structure it in a way that allows or even promotes reuse of as much of it as possible in the first place. Then the non-private parts may not become as much of a burden in the future as some fear.
A lot of (framework) code I see adopts an inconsistent use of private: protected, non-final methods which barely do anything more than delegating to a private method is commonly found. protected, non-final methods whose contract can only be fulfilled through direct access to private fields too.
These methods cannot logically be overridden/enhanced, although technically there's nothing there to make that (compiler-)obvious.
Want extendability and inheritance? Don't make your methods private.
Don't want certain behavior of your class altered? Make your methods final.
Really cannot have your method called outside of a certain, well-defined context? Make your method private and/or think about how you can make the required well-defined context available for reuse through another protected wrapper method.
That's why I advocate to use private sparingly. And to not confuse private with final. - If a method's implementation is vital to the general contract of the class and thus is must not be replaced/overridden, make it final!
For fields, private is not really bad. As long as the field(s) can be reasonably "used" via appropriate methods (that's not getXX() or setXX()!).
4
Possible duplicate of Why is Clean Code suggesting avoiding protected variables?
– gnat – 2016-03-11T09:21:10.817229To downvoters: While I also strongly disagree with the OP's premises, I am upvoting this question because it is perfectly coherent and worth answering. Yes, the OP needs to be told why this is wrong, but the way to do that is to write an answer (or suggest edits to existing answers), not to downvote just because he hasn't figured it out for himself yet. – Ixrec – 2016-03-11T09:51:47.240
18Derived classes are part of the outer world. – CodesInChaos – 2016-03-11T10:40:56.813
19Don't forget that protected doesn't always mean access is locked to the inheritance hierarchy. In Java, it grants package level access also. – berry120 – 2016-03-11T11:49:36.210
2"sooner or later, you are going to make a subclass of a class and in that case" - Ähm ... no. – Jens Schauder – 2016-03-11T13:00:18.793
2I have voted to close as too board, as this can not be answers in a way that makes sense for ALL object oriented languages. – Ian – 2016-03-11T13:15:03.020
Encapsulation means you have to use less brain ram when you develop. – Mark Rogers – 2016-03-11T16:35:11.140
I see many comments in our code: "// need to make this protected because ...". And I've had change to protected often enough. However, if we (and y'all out there!) simply had better classes - really sticking the Single Responsibility Principle - then other mechanisms and design patterns could more effectively address extendability issues. – radarbob – 2016-03-11T18:19:07.680
7My professor used to say that "There are things I wouldn't tell my children. Those I my private fields." – SáT – 2016-03-11T18:52:59.300
Does
protectedin general imply it is also visible to the package, or is this only a Java thing? – Captain Man – 2016-03-11T20:33:10.807@CaptainMan C#
– abluejelly – 2016-03-11T21:42:19.363protectedimplies only accessible to the class and its descendants. Swift doesn't have aprotected, but itsinternalbehaves like theprotectedyou describe. If you wanna read Apple's devblog on it, it actually makes a pretty good case for both why they called itinternaland why the inheritance-basedprotectedis pretty meh2It might interest you to learn that Python doesn't have a concept of
privateorprotected. There is a naming convention for warning callers of your code not to mess with stuff, but there's no actual prevention. This sounds insane if you are coming from a language like Java or C# that does have these and encourages you to use them, but if you give it a chance, you'll find that it's not really that big a deal. Note that this doesn't mean that Python programmers recommend leveraging this fact in normal situations. It just means you need to know what is a good idea and what's stupid. – jpmc26 – 2016-03-11T22:26:51.897@jpmc26 Actually another note with that, it doesn't really warn it so much as Python will not help the coder discover/access/understand any member that starts with.... One underscore? It's been a while. – abluejelly – 2016-03-11T22:38:50.597
@abluejelly Yes, single underscore, double underscore in front for name mangling, double underscore in front and at the end for magic methods. It depends on how you look. If you do
dir(o)on anything, you'll get everything (even the name mangled attributes and magic methods). I think you're thinking offrom x import *statements, which don't import attributes on the module starting with an underscore. – jpmc26 – 2016-03-11T22:43:12.377@jpmc26 Couldda sworn there was a
manorhelpor something that just printed the__doc__, but failed on hidden. But I very well might be thinkingfrom x import *. Was a long time ago that I messed with python. – abluejelly – 2016-03-11T23:02:10.5371
don't remove this please... http://steve-yegge.blogspot.com.au/2010/07/wikileaks-to-leak-5000-open-source-java.html
– imel96 – 2016-03-14T18:46:36.950Also related: http://stackoverflow.com/q/18168410/1394393
– jpmc26 – 2016-03-14T22:03:42.053You can make any protected method be "public" by inheriting the class and in the child make a public method which calls the protected method of its parent. – Marian Spanik – 2016-03-16T13:17:32.287
"In OOP, sooner or later, you are going to make a subclass of a class and in that case, it is good to understand and being able to modify the implementation completely." there's a whole truck-load of assumptions here, and I don't agree with either of them. why do you think this? – kai – 2016-03-16T13:24:15.853
Shouldn't it be "Private by community"? :) – GordonM – 2016-03-18T14:37:43.150
2Python's convention is "we're all consenting adults here". I can honestly say that I've never been in a situation where I've thought, "Boy, I'm so glad they made that variable private!" On the other hand I've had several instances where a developer decided that a variable should be private because information hiding!!11!1! And my life was made more difficult. – Wayne Werner – 2016-03-20T14:16:50.673