tags:

views:

437

answers:

8

If I'm writing a class, when do I make a method private, versus protected? In other words, how I can know in advance that a client programmer would never ever need to override a method? In a case where it's something that has external considerations, like a database connection?

A: 

In other words, how I can know in advance that a client programmer would never ever need to override a method?

If you don't know assume they will need to. If that's fine by you (ie, if you think they should be able to) then use protected; otherwise use private.

Andreas Bonini
I guess my question is, how would I know? I feel like asking myself that is akin to divining the future. How could I know the needs of some future programmer? It seems like what you are telling me is to always use protected, since I haven't yet been in a situation where I *know* that a client programmer will never need to.
You can't "KNOW", you can only make an educated assumption based on your knowledge of the app and the developers who will use/consume it.
brendan
I don't agree. It's better to assume they will not need to override it. Keep it private until proven otherwise!
Martin Wickman
Do you have a requirement to expose certain functions outside your system?
Kwebble
+2  A: 

Don't think of the private/protected/public thing as if a programmer would ever "need" a method. Think of it as if you want to allow them access to it.

If you think they should be allowed to change the DB Connection String then make it public.

Robin Day
So should the default be protected, and private only when I'm really sure they should never have access?
If I'm forced to then I'll say go with "private" as default as per brendan's answer, however, I don't actually think there should be a "default" as such. You really need to think about what it is the method does. Then you decide what it should be.
Robin Day
A: 

Private members are used to encapsulate the inner workings of your class. Use them to hold data that only you want to be able to access. For example, let's say you have a field called _name and a getter/setter named GetName()/SetName(name). Maybe you want to do some syntax checking on name before you allow the SetName to succeed, else you throw an exception. By making _name private, you ensure that this syntax checking will occur before any changes to name can occur (unless you yourself change _name in your own class, in your own code). By making it protected, you're saying to any potential future inheritor of your class, "go ahead and monkey with my field."

In general, protected is used sparingly and only in specialized cases. For example, you might have a protected constructor that exposes some additional construction functionality to child classes.

David Pfeffer
+4  A: 

I typically will start at the lowest level. If you're unsure make it private. Then as needed you can make things protected or public.

The idea being it is not a breaking change to go from private to protected but it could be a breaking change to go the other way.

brendan
This is agood thought, but what about encapsulation? If I'm writing a child class and I need to use a private method in parent, here I am mucking around with parent's code.
Right. Alot depends on the specifics of your app. You will be setting the rules as to what can/cannot be overriden or called from the child. Some parts will be so core to the functionality you will need them to be private to protect future developers from accidentially overriding/calling them. Others will be optional or configurable and should be exposed as protected or public where appropriate.
brendan
+2  A: 

I always make all methods private as default. This is to keep the interface clean and easy to maintain.

It is much harder to change or hide an already visible method than to make a private method more visible. At least if you need to be compatible with existing client code.

Martin Wickman
+9  A: 

public and protected methods form the 'interface' to your object, public for developers using (delegating to) your class, and protected for developers wishing to extend the functionality of your object by subclassing it.

Note that it's not necessary to provide protected methods, even if your class will be subclassed.

Both public and protected interfaces need careful thought, especially if this is an API to be used by developers outside your control, since changes to the interface can break programs that make assumptions about how the existing interface works.

private methods are purely for the the author of the object, and may be refactored, changed and deleted at will.

I would go for private by default, and if you find you need to expose more methods, have a careful think about how they will be used - especially if they are virtual–what happens if they are replaced completely with an arbitrary alternative function by another developer–will your class still work? Then design some appropriate protected which are useful for developers subclassing your object (if necessary), rather than exposing existing functions.

Alex Brown
+1 the point is that you have to think about your implementation in another way when you make private stuff protected. You can't just make everything protected/public.
tanascius
/\-- +1 Took the words out of my mouth.
Grant Palin
A: 

I typically just make everything private and refactor when I need to call it from a base class.

Except when I feel lazy and do everything protected that isn't definitely dangerous.

John
+1  A: 

In other words, how I can know in advance that a client programmer would never ever need to override a method?

You cannot. And you don't need to. It is not your job to anticipate IF a developer might want to override a method, let alone how. Just assume he wants to and enable him to do so without having to touch your code. And for this reason, do not declare methods private if you don't have to.

If a developer feels he needs to adjust some functionality of your classes, he can pick from a number of structural and behavioral patterns to do so, e.g. Decorators, Adapters or by subclassing. Using these patterns is good, because it encapsulates the changes into the developer's own class and leaves your own code untouched. By declaring methods private, you make sure the developer will monkey with your class. And that is bad.

A perfect example is Zend Framework's DB adapter. They discourage the use of persistent connections and their adapters provide no mean to this end. But what if you'd want to have this nonetheless and the adapter method was marked private (it isn't, but what if)? Since there is no way to overwrite the method, you would (yes, you would) change the adapter code right within it's class or you'd copy & paste the code into your own adapter class, effectively duplicating 99% of the class just to change a single function call. Whenever there is an update to this adapter, you either would lose your changes or you wouldn't get it (in case you c&p'd). Had it been marked protected (as it is), you could just have written a pConnectAdapter subclass.

Moreover, when subclassing, you are effectively saying subClass is a parentClass. Thus, you can expect the derived class to have the same functionality as the parentClass. If there is functionality in the parentClass that should not be available in the subClass, then disabling it conceptually belongs to the subClass.

This is why I find it much better practise to default all methods and properties to protected visibility and only mark those methods (not properties though) supposed to allow interaction with my class from another class or script as public, but only a few things private. This way, I give the developer the choice of using my class as I intended it to be used and the option to tweak it. And if he breaks something in the process, it is very likely his fault then, not mine.

Gordon
"And for this reason, do not declare methods private if you don't have to." Well, I never *have* to -- I'm always *choosing* to. The class by itself would work just fine whether the method is `private` or `protected`. So the *have to* would come in only when I'm anticipating the needs of a future client programmer. Unless I want to ensure that some purely internal functionality of the parent class can never be broken by a client programmer -- is that what you're saying? Unless that's the case, go with `protected`?
Yes, I guess so. I am quite aware that there is a lot of people that prefer to use private as default and expose only what is needed when it is needed. But with future developers in mind, you just cannot anticipate what they will need. Feel free to do it differently though. There was a similar question a few months back that you might like to read too: http://stackoverflow.com/questions/419844/best-to-use-private-methods-or-protected-methods
Gordon