Protected Member in Final Class

ID

java.protected_in_final_class

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Java

Tags

code-style

Description

Reports protected methods and fields declared in a final class.

Rationale

final on a class forbids subclassing. Inside a non-subclassable class, protected is functionally identical to package-private — there are no subclasses to whom the additional visibility could matter. Mixing the two sends a contradictory signal: "this class is closed for extension and I want subclasses to see this member".

// Bad
public final class Settings {
    protected int timeout;       // protected is meaningless here
    protected void reset() {}    // protected is meaningless here
}

Remediation

Change protected to the visibility that actually reflects the design (typically private or package-private), or remove the final modifier from the class if subclassing is intended.

// Good
public final class Settings {
    private int timeout;
    private void reset() {}
}