So what do you think?

My Java lecturer said that properties should always be declared private and there is no reason why it should be done any other way. I immediately started to think inheritance, how it would affect to that.

2

There are 2 best solutions below

8
On BEST ANSWER

Do you mean declaring the field to be private? If so, I agree with your lecturer, although "no reason" may be slightly overkill. There are very occasional reasons to use non-private fields - such as in private nested classes.

But yes, in a simple superclass/subclass relationship I would use getters/setters instead of making the field protected. It separates the implementation from the API exposed by the class - even to its subclasses.

3
On

Private would prevent classes that extend the original class from accessing it directly. Generally speaking I think that protected is a better way to declare a variable if you expect to extend your original class and want the inheriting class to be able to directly modify that particular class member.