The difference between private and protected visibility is that child classes can see and use protected
members, but they cannot see private ones. Since a final class will have no children, marking the members of a
final class protected is confusingly pointless.
Note that the protected members of a class can also be seen and used by other classes that are placed within the same package, this
could lead to accidental, unintended access to otherwise private members.
Noncompliant code example
public final class MyFinalClass {
protected String name = "Fred"; // Noncompliant
protected void setName(String name) { // Noncompliant
// ...
}
Compliant solution
public final class MyFinalClass {
private String name = "Fred";
public void setName(String name) {
// ...
}
Exceptions
Members annotated with @VisibleForTesting annotation are ignored, as it indicates that visibility has been purposely relaxed to
make the code testable.
public final class MyFinalClass {
@VisibleForTesting
protected Logger logger; // Compliant
@VisibleForTesting
protected int calculateSomethingComplex(String input) { // Compliant
// ...
}
}