theapache64
theapache64

Reputation: 11744

What is the purpose of @SuppressWarnings("hiding") in eclipse?

I am new in programming. I was looking at this answer and found a list of possible values for the @SuppressWarnings annotation. but I can't figure out the usage of the value hiding. Can anyone help me with an example?

Upvotes: 8

Views: 4321

Answers (1)

DamienBAus
DamienBAus

Reputation: 268

From xyzws,

A class can declare a variable with the same name as an inherited variable from its parent class, thus "hiding" or shadowing the inherited version. (This is like overriding, but for variables.)

So hiding basically means that you've created a variable with the same name as a variable from an inherited scope, and the warning is just letting you know that you've done it (in case you needed access to the inherited variable as well as the local variable).

An example is:

public class Base {
    public  String name = "Base";
    public  String getName() { return name; }
}


public class Sub extends Base {
    public String name = "Sub";
    public String getName() { return name; }
}

In this example, Sub hides the value of name given by Base with it's own value - "Sub". Eclipse will warn you - just in case you needed the original value of the variable name - "Base".

Upvotes: 11

Related Questions