Javatpoint Logo
Javatpoint Logo

Shadowing in Java

Shadowing is the concept of OOP paradigm. It provides a new implementation to the base member without overriding it. Both shadowing and hiding are the same concepts but uses in different context. These are compile-time process. In this section, we will discuss the concept of variable and method shadowing in Java with example.

What is shadowing in OOP?

Shadowing is a computer programming phenomenon in which a variable declared in one scope (like decision block, method, or inner class) has the same name as another declaration of the enclosing scope. In this case, the declaration shadows the declaration of the enclosing scope.

In simple words, the original implementation of the base class member gets shadowed (hidden) with the new implementation of the base class member provided in derived class. Shadowing is actually hiding overridden method implementation in derived class and call the parent call implementation using derived class object.

Also, note that the shadowed element is not available for reference, instead when the code uses the element name, the compiler resolves it to the shadowing element. We cannot refer to a shadowed declaration by its name alone. The concept is just like masking so, it is also known as name masking.

Variable Shadowing and Variable Hiding

Java allows us to declare three type of variables:

  • Local Variables
  • Instance Variables
  • Class Variables

If the instance variable and the local variable declared with the same name and we want to print the instance variable, in this case, it will print the local variable instead of instance variable.

When we print the variables declared inside the method, the local values will be printed on the console. Hence, we can say that local variables shadowing the instance variables.

Let's understand the concept of shadowing through a Java program.

In the following program, we have defined two variables i.e. name and age and the same is also defined inside a method display(). The variables defined just after the class declaration are called instance variable and the variables declared inside the method are called local variable.

Shadowing.java

Output:

Car Name: Bugatti
Price: 43000000

What if want to access instance variables in a method also? We can access instance variables by using this keyword. Consider the following program.

Shadowing.java

Output:

Car Name: Bugatti
Price: 43000000
Car Name: Ferrari
Price: 50000000

Let's see another Java program, for the same.

Shadow.java

Output:

x = 12
this.x = 1
Shadow.this.x = 0

Method Shadowing

The concept of method shadowing is the same as the concept of method overriding. The instance method is defined in both the classes i.e. superclass and in the subclass. When we invoke the method of the subclass, the superclass method is overridden by the subclass. The concept is known as method shadowing.

MethodShdowing.java

Output:

display() method of the subclass invoked.

Note that a static method cannot be shadowed by an instance method because it leads to ambiguity. But the static variable can be shadowed by an instance variable (only for subclass). Consider the following code snippet.

Conclusion

Shadowing and hiding in OOP are the two different words for the same thing but differ in context.

Hiding is associated with polymorphism while shadowing is not. When we talk about the term hiding, it means we are talking about inheritance where derived class method hides a base-class method from the normal inherited method call chain.

On the other hand, when we talk about shadowing, it means, we rake talking about scope (like decision block, method, or inner class). Sometimes hiding is also called shadowing.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA