Variable Shadowing and Variable Hiding in Java with Examples

In this tutorial, I will be sharing what is Variable Shadowing and Variable Hiding in Java with examples. Before diving deep into the topic, we must be aware of the type of variables and there scopes. There are 4 types of variables in Java.

Read Also:  Access Modifiers in Java

Types of Variables

Instance variables:

The variables which are declared inside the class but outside the body of the method are called the Instance variables. They are also known as Non-static fields.

Local variables:

The variables which are declared inside a method or constructor are called Local variables.
a. 'static' keyword can not be used with local variables.
b. The local variables are only visible to the method in which they are declared.
c. The local variables are not accessible from the rest of the class.


variable shadowing in java


Class variables:

The variables which are declared as static are called Class variables. They are also known as static variables. When you defined a variable as static than a single copy of it is created. The single copy of the static variable is shared among all the instances of a class.

Parameter variables:

Arguments to methods, constructors etc. are called as parameter variables. The main difference between Local variable and Parameter variable is that Parameter variables are always defined at the signature of constructor or method where as Local variables are always defined inside method or constructor.  

class Test
{
String instanceVar = "It represents Instance Variable"; // Instance variable
static String classVar = "It represents Class Variable"; // Class or Static variable
public String getLocalVar()
{
String localVar = "It represents Local Variable";// Local variable
return localVar;
}
}

public class JavaHungry
{
public static void main(String[] args)
{
// Creating an object of Test class
Test t = new Test();
System.out.println(t.instanceVar);
System.out.println(Test.classVar);
System.out.println(t.getLocalVar());
}
}

Output:
It represents Instance Variable
It
represents Class Variable
It
represents Local Variable 

What is Variable Shadowing

According to Wikipedia, When a variable declared within a certain scope has the same name as a variable declared in an outer scope.

In other words, if we declare a local variable having the same name as of the instance variable, the local variable shadows the instance variable inside the block in which it is declared. This is called Variable Shadowing. The example of variable shadowing is given below:

public class JavaHungry
{
String name = "John"; // Instance variable
int age = 21; // Instance variable

public void show()
{
String name = "Roger"; // Local variable
int age = 30; // Local variable
System.out.println("Name: "+ name);
System.out.println("Age: "+ age);
}

public static void main(String[] args)
{
JavaHungry obj = new JavaHungry();
obj.show();
}
}

Output:
Name: Roger
Age: 30


If you still, need to access the instance variables inside the method, then, you can access it using this keyword as shown below.

public class JavaHungry
{
String name = "John";
int age = 21;

public void show()
{
String name = "Roger";
int age = 30;
System.out.println("Name: "+ name);
System.out.println("Age: "+ age);
System.out.println("Name: "+ this.name);
System.out.println("Age: "+ this.age);
}

public static void main(String[] args)
{
JavaHungry obj = new JavaHungry();
obj.show();
}
}

Output:
Name: Roger
Age: 30
Name: John
Age: 21


What is Variable Hiding

Whenever we declare a variable in the child class with the same name as one we defined in the parent class, even if their types are different, the variable inside the child class hides the inherited version. This is termed as Variable Hiding.

In simpler words, when a class inherits another class, if the parent class and child class both have variable declarations with the same name then the variable declared inside child class will hide the inherited version.

variable hiding in java
In the above image, we can see that the Child class extends the Parent class. Both classes have a declaration for variable a of type int. In this case, the variable inside the Child class will hide the inherited version.

class Parent
{
int a = 10;
String str = "Parent Class";
}

class Child extends Parent
{
int a = 100;
String str = "Child Class";
}

public class JavaHungry
{
public static void main(String args[])
{
Child obj = new Child();
System.out.println("Value of variable a is: "+obj.a);
System.out.println("Value of variable str is: "+obj.str);
}
}

Output:
Value of variable a is: 100
Value of variable str is: Child Class


In the above example, the Child class and Parent class both have variables with the same name ‘a’ and 'str'. The variables declared within the Child class hide the inherited variables of the Parent class.

Special case:

What do you think will be the output of the following code:

class Parent
{
int a = 10;
String str = "Parent Class";
}

class Child extends Parent
{
int a = 100;
String str = "Child Class";
}

public class JavaHungry
{
public static void main(String args[])
{
Parent obj = new Child();
System.out.println("Value of variable a is: "+obj.a);
System.out.println("Value of variable str is: "+obj.str);
}
}

Output:
Value of variable a is: 10
Value of variable str is: Parent Class


You might be surprised. The above output has nothing to do with shadowing/hiding. In Java, variables are resolved by the reference type and not the object they are referencing

Difference between Variable Shadowing and Variable Hiding:

1. Variable Shadowing happens within the same class whereas Variable hiding happens within a parent-child class pair.

2. Variable Shadowing happens when an instance variable and a local variable have the same name within a class whereas Variable Hiding happens when Parent Class and Child Class have instance variables with the same name.

That's all for today. Please mention in comments in case you have any questions related to variable shadowing and variable hiding in java with examples.

You may also like:

Polymorphism in Java
Method Overloading in Java
Method Overriding in Java
Difference between Method Overloading and Method Overriding in Java
 
References:
Variable Java Docs
Shadowing Java Docs
Hiding Java Docs 

0 Comments