Variable Hiding or Shadowing
In Java variable hiding or Shadowing happens when a variable is being hidden or shadowed by another variable with same name which is closer in scope. So if you refer the variable simply by its name, will use the variable which is closest in scope.
--> A variable declared in method has closer scope than the one declared at class level.
--> And a variable declared at class level has closer scope than the one declared in its super class.
See below diagram:
Scenario 1: When local variable shadows instance variable
When there is a local variable in a method (either declared or passed in as a parameter) and another variable with same name is declared at class level then the local variable shadows the instance variable.
package com.meritmix.blog.java;
class TestClass {
public String name = "Base";
public void setName(String name) {
name = name; //this is not the correct assignment as variable 'name'
// refers to local variable and here both left hand side
// and right hand side refer to local variable.
this.name = name;// this one is correct as to overcome variable hiding
// and to access instance variable we need to use 'this'
// keyword.
}
}
public class VariableHidingTest {
public static void main(String[] args) {
TestClass s = new TestClass();
s.setName("Ravi");
}
}
Scenario 2: When instance variable shadows variable inherited from parent class
package com.meritmix.blog.java;
class Base {
public String name = "Base";
public void printName() {
System.out.println(name);
}
}
class Sub extends Base {
public String name = "Sub";
public void printName() {
System.out.println(name);//as instance variable is hiding variable
// inherited from parent class, this will refer
// instance/class level variable and print 'Sub'
System.out.println(super.name);//to access inherited variable we need
// to use 'super' keyword, it prints 'Base'
}
}
public class VariableHidingTest {
public static void main(String[] args) {
Sub s = new Sub();
s.printName();
}
}
Given the fact that variables are bound statically (static binding), instead of using super keyword we can typecast the sub class object to parent class type and access the parent class variable.
package com.meritmix.blog.java;
class Base {
public String name = "Base";
public void printName() {
System.out.println(name);
}
}
class Sub extends Base {
public String name = "Sub";
public void printName() {
System.out.println(name);//as instance variable is hiding variable
// inherited from parent class, this will refer
// instance/class level variable and print 'Sub'
System.out.println(((Base)this).name);//typecast current object to parent type
// and access variable( static binding)
}
}
public class VariableHidingTest {
public static void main(String[] args) {
Sub s = new Sub();
s.printName();
}
}
In below example method level variable hiding class level variable and class level variable is hiding inherited variable from parent class.
package com.meritmix.blog.java;
class Base {
public int a = 30;
public void print() {
System.out.println(a);
}
}
class Sub extends Base {
public int a = 20;
public void printName(int a) {
System.out.println(a);//prints local variable
System.out.println(this.a);//prints instance/class level variable
System.out.println(super.a);//prints class level variable
}
}
public class VariableHidingTest {
public static void main(String[] args) {
Sub s = new Sub();
s.printName(10);
}
}

No comments:
Post a Comment