Search code examples
javaeclipseintellij-ideaprivatecode-inspection

Private field in parent class shadowed/hidden by subclass - inspections to catch it


I just cracked a tough bug which turned out to be a result of the following scenario:

class Parent {
    private Object field;

    public Object getField() {
        return field;
    }

    public void setField(Object field) {
        this.field = field;
    }
}

class Child extends Parent {

}

class GrandChild extends Child {
    private Object field;

    public Object getField() {
        return field;
    }
}

Now I am fully aware that this is a nasty smell. It took me several days to track down. What I am looking for is an inspection that might highlight this or similar scenarios elsewhere in code. Eclipse or IntelliJ or CheckStyle or Findbugs or anything would be fine.

Essentially I would like to find private fields that shadow parent private fields.


Solution

  • It's a specific compiler warning within eclipse.oxygen
    enter image description here