crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Can you define variable shadowing in Java with example?
Question Posted on 08 Aug 2021Home >> Java >> Java Questions >> Can you define variable shadowing in Java with example?

Can you define variable shadowing in Java with example?
If any of instance variable and the local variables have the common same name. And when we access this variable then it will print the local variable which will shadowing the instance variable. Below are the some reasons for considering a variable shadowing.
(i)When method is overridden in the child class.
(ii)When subclass declares a variable with same name of the parent class variable.
(iii)There are some situation where a variable in local scope with same name as the name of a variable defined in and instance scope.
Below is the example for variable shadowing

public class testShadowing
{
 string name="interviewqsn";
 int subject=12;
 public void displaylocal()
 {
  string name="crackyourinterview";
  int subject=20;
  System.out.println("Name:-" + name);
  System.out.println("subject:-" + subject);
 }
 public static void main(String args[])
 {
  new testShadowing().displaylocal();
 }
}


Output:-
Name:- crackyourinterview
subject:- 20
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

What are the main difference between Array and Arraylist

What is classloader in Java and name of some classloader

Different modules of the Spring framework

Difference between Object and Class

Most important 15 aspects that Java Developer should learn in year 2020






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap