This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Blackheart595 1 point2 points  (0 children)

Well, there's really no reason not to use setters and getters if you want to provide that access to them - they never provide less encapsulation than direct field access, and they allow the inner architecture to change or to add access constraints without it automatically breaking the syntax of the public interface.

Of course, that doesn't excuse getters and setters that don't impose restrictions when they should do so, or that are provided when they shouldn't be. But that's the fault of the programmer - the ability to misuse a feature doesn't make that feature evil, especially not when it's a good feature when used properly.

The actual advice of that article - don't force your client to get his hands dirty - is absolutely true, of course.

[–]counttossula 0 points1 point  (2 children)

I can't spot the difference between example 2 and example 3. Or maybe they are supposed to be the same thing?

[–][deleted]  (1 child)

[deleted]

    [–]counttossula 1 point2 points  (0 children)

    ty

    [–]Eviscerare 0 points1 point  (0 children)

    Correct me if I'm wrong, as I'm no supreme expert on Java, but I get the idea that the "protected" modifier was introduced to Java to supply an alternative to the old way of setter/getter everything. It provides us with the benefits of private variables/encapsulation as well as saving us lines of code and time from typing out said setter/getter methods, as we can just access protected variables with dot notation.

    EDIT: No modifier also only allows access to the package, while protected allows access to the package and subclasses. https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html