The difference between private
and protected
visibility is that child classes can see and use protected
members but 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, keeping the same visibility as for the base class is standard practice. This rule ignores
protected
functions in a final
class that override base class protected
functions.