OldCurmudgeon
OldCurmudgeon

Reputation: 65813

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.

Upvotes: 1

Views: 420

Answers (2)

OldCurmudgeon
OldCurmudgeon

Reputation: 65813

Inspired by Robert's post I found it in IntelliJ:

IntelliJ equivalent

Upvotes: 2

Robert Kock
Robert Kock

Reputation: 6008

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

Upvotes: 0

Related Questions