Does checkstyle has rule which checks if subclass overrides public or protected field of a parent class.
For example
class Ancestor {
public static final int VALUE = 123;
}
class Descendant extends Ancestor {
public static final int VALUE = 100; // <-- this is unwanted
}
I want somehow with checkstyle to forbid such situation.
Override semantics are not applicable for static fields. When you access
VALUEinDescendantthen 100 is used as the value. You can also reference superclass’s VALUE usingAncestor.VALUE.Now, the best way to avoid confusion is to always access static members by qualifying it with class name like
Ancestor.VALUEandDescendant.VALUE. The IDE’s (like eclipse) let you enforce this rule but didn’t see this in any of static code analysis tools. Below is the screenshot on how to enforce this in eclipse