I was recently given advice to change all my protected member variables in the base class to private access.
But the only advantage that comes to mind in terms of real usage is to just be able to give separate Read, Write and Read/Write access to them to protect against misuse. The following Java code details this use.
public class BaseClass {
protected Object protectedMember;
private Object privateMemberR;
private Object privateMemberW;
private Object privateMemberRW;
public BaseClass() {
}
protected final Object getPrivateMemberR(Object obj){
return privateMemberR;
}
protected final void setPrivateMemberW(Object obj){
privateMemberW = obj;
}
protected final Object getPrivateMemberRW(Object obj){
return privateMemberRW;
}
protected final void setPrivateMemberRW(Object obj){
privateMemberRW = obj;
}
}
Maybe declaring them final is not always needed, only to be sure that they are not overwritten and misused.
I read similar arguments like this Clean code questions, but if I could have gotten away with private member variables, I would have made them private in the first place.
Furthermore, since at least in Java you can only have the access modifier for an overwritten method to allow more, but not less, access than the superclass method, you are still gonna drag either protected member variables or protected getters and setters all the way up in your subclasses.
I have not considered multiple inheritance since Java does not allow it, but still I do not see how using standard notation for getters and setters you could avoid running into problems.
Is there any other actual benefit in terms of real usage (other than it looks more pretty and easier to read, magically hope you protec against improper use by replacing direct reference with getters and setters)