Why is this an issue?
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.
Noncompliant code example
class C final {
protected: // Noncompliant
void fun();
};
Compliant solution
class C final {
private:
void fun();
};
Exceptions
When overriding a base class function, it is common practice to keep the same visibility as for the base class. This rule ignores
protected
functions in a final
class that are overrides of a base class protected
function.