Reputation: 55714
When using Java, is there a way to get a compiler warning (using Eclipse, if it matters) when a child class shadows a super class's instance variables by declaring another one of the same name? For example:
class A {
String variable;
A() {
variable = "A";
}
}
class B extends A {
int variable;
B() {
variable = 1;
}
}
B b = new B();
System.out.println("Variable value: " + b.variable + ", " + ((A) b).variable);
// prints out: "Variable value: 1, A"
I'd like to be warned in B
that variable already exists.
Upvotes: 3
Views: 515
Reputation: 24262
Yes. In Eclipse go to
Preferences->Java->Compiler->Errors/Warnings
In that pane there is a subsection on Name shadowing and conflicts that contains the options you are looking for.
Upvotes: 6