Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java protected fields vs public getters

Tags:

java

What is better practise and why: accessing base class variables through a protected field or a public getter on the private field.

(The getter will be public regardless)

like image 832
DD. Avatar asked Feb 17 '10 09:02

DD.


People also ask

Should getters be public or private?

In general, they should be public. If they are private they can only be called from within your class and, since you already have access to the private variables within your class, are redundant. The point of them is to allow access to these variables to other, outside, objects.

Can getters and setters be protected?

If you use setters/getters, you can allways add validation, checking etc later on. You can also only provide a getter to make a variable read only. If someone exposes a variable, they should treat it as unchangeable as much as they should a public/protected method.

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

Should accessor methods be private?

You should only use accessors for fields that need to be accessed. protected simply expands the accessibility, and you should consider using a protected getter instead of a field (to allow yourself to move the field around without breaking code if needed).


2 Answers

If there's going to be a public getter anyway, why would you want to expose the field itself more widely than absolutely necessary? That means it's immediately writable by subclasses (unless it's final to start with).

Personally I like all my fields to be private: it provides a cleaner separation between API and implementation. I regard the relationship between a superclass and a subclass as similar to that of a caller and callee - changes to the underlying implementation shouldn't break subclasses any more than they should break callers. The name of a field is an implementation detail which shouldn't impact other classes.

Admittedly my view is occasionally seen as somewhat extreme...

like image 64
Jon Skeet Avatar answered Oct 05 '22 18:10

Jon Skeet


You should always program against the public API of a class, that is, use the public methods.

The reason is simple. Someday in the future, you or someone else might want to change the implementation. This should always be possible. If you rely on instance variable, you limit yourself.

Also, when accessing the variable, you can not control if that variable is read-only nor can you add checks when this variable is changed.

If you use setters/getters, you can allways add validation, checking etc later on. You can also only provide a getter to make a variable read only.

like image 22
phisch Avatar answered Oct 05 '22 18:10

phisch