Java allows us to declare many types of variables, but although there are many types, variables can always be categorized into the following categories:

  1. Instant Variables – Variables (fields) are defined inside a class and have object level scope;
  2. Class Variables – Variables defined static in a class that are shared by all class instance objects;

Are defined inside a class with static keyword, These variables have a class level scope and are common to all objects of the same class.

3. Local Variables – Variables defined in methods or conditional blocks have block-level scope and can only be accessed in the block domain where they are defined;

class Parent {# # #//Declaring instance variable by name 'x'
    String x = "Parent's Instance Variable" ; 
    
    public void printInstanceVariable(a){ System.out.printn(x); }
    
    public void printLocalVariable(a){
       //Shadowing instance variable 'x' by a local variable with same name 
       String x = "Local Variable"; 
       System.out.println(x); 
       
       //if we still want to access instance Variable , we do that by using 'this.x'
       System.out.println(this.x) ; }}class Child extends Parent {
    // Hiding Parent class's variable 'x' by defining a variable in child class with same name 
    String x = "Child's Instance Variable ";
    
    @Override 
    public void printInstancVariable(a){
        System.out.print(x);
        
        //If we still want to access variable from super class ,we do that by using 'super.x'
        System.out.print(","+super.x+"\n"); }}Copy the code

# What is Varable-Shadowing

Variable-shadowing occurs when we define a Variable a in a closed inline code block, while the outer code block has a Variable with the same name. In other words, when local-variable and instance-variable have the same name, local-variable Shadow the external instance-variable in the method block. For example, in the following example, instance-variable x is shaded by local-variable x in printLocalVariable();

class Parent {# # #//Declaring instance variable by name 'x'
    String x = "Parent's Instance Variable" ; 
    
    public void printInstanceVariable(a){ System.out.printn(x); }
    
    public void printLocalVariable(a){
       //Shadowing instance variable 'x' by a local variable with same name 
       String x = "Local Variable"; 
       System.out.println(x); 
       
       //if we still want to access instance Variable , we do that by using 'this.x'
       System.out.println(this.x) ; }}Copy the code

What is a Variable – Hiding

Variable-hiding occurs when we define a field (Variable) in a subclass that has the same name as the parent class, whereby a Variable is declared in the subclass that has the same name as a Variable inherited from the parent class. When variable-hiding occurs, the subclass is Hiding the Variable it inherits from the parent class.

In other words, when both a subclass and its parent have a variable of the same name, the subclass variable hides the parent variable;

In the following example, we hiding the variable X inherited from the parent in child

class Child extends Parent {
    // Hiding Parent class's variable 'x' by defining a variable in child class with same name 
    String x = "Child's Instance Variable ";
    
    @Override 
    public void printInstancVariable(a){
        System.out.print(x);
        
        //If we still want to access variable from super class ,we do that by using 'super.x'
        System.out.print(","+super.x+"\n"); }}Copy the code

Variable-hiding and method-overriding are not the same thing

Variable-hiding and method-looking may look like the same thing, but they are not really the same thing, as tant is only about methods, and tant is simply about variables.

Now, when we talk about method-overriding, methods of subclasses override methods inherited from their parents, that is, Overriding methods don’t know much about superclasses anymore, they’re all on themselves; So that when we use a reference variable of a parent type to point to an instance object of a subclass, we are accessing only the methods of the subtype.

However, in variable-hiding, subclasses simply hide, rather than overwrite, the inherited Variable. When a reference variable of a parent type refers to an instance object of a subclass, we access fields (variables) inherited from the parent class.

When a reference Variable of the parent type refers to an Instance object of a subclass whose instance-variable has the same name as the parent class’s instance-variable, the Instance accessed by the reference Variable will be the parent class’s Instance object

    public class EntertanceClass{
	public static void main(String[] args ) throws Exception {
		
		Parent parent = new Parent();
		parent.printInstanceVariable(); // Output - "Parent's Instance Variable"
		
		System.out.println(parent.x);  //Output - 'Parent's Instance Variable'
		
		Child child = new Child() ;
		child.printInstanceVariable(); // Output - "Child's Instance Variable , Parent's Instance Variable"
		System.out.println(child.x); // Output - "Child's Instance Variable"
		
		parent = child ; // Or parent = new child();
		parent.printInstanceVariable();  // Output - "Child's Instance Variable , Parent's Instance Variable"
		System.out.println(parent.x);	// Output - "Parent's Instance Variable"
		
		// Accessing child 's variable from parent 's reference by type casting 
		System.out.println(((Child)parent).x );  // Output - 'Child's Instance Variable'}}Copy the code