Java – Best Practices for Using Public, Protected, Private

design-patternsencapsulationjavaobject-oriented

Is it fair to say that it is good practice to default everything to private up front when coding something?

And then only upgrade it to protected if a subclass needs it, or public if another class needs it?

Best Answer

Short answer: Yes

Longer answer:

Yes, but that shouldn't be interpreted as a suggestion to start by writing your classes with everything private; that approach implies class design by focusing on the implementation detail before you've settled on an interface.

One of the most important aspects to consider when designing a class is how it will be used; which involves thinking about your public methods before you start thinking about private/implementation details.

Furthermore, that approach is usually missing out on chances to ask yourself "How would I write a unit test for this class?" - which is an important question to ask even if you aren't actually writing unit tests. (Related: "What are the design principles that promote testable code?" )

So, once you have defined the public interface, then it is a good idea to default the rest to private because most of that will typically be gritty implementation detail which is of no concern to anything outside of the class.