Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaShadowing in Java

Shadowing in Java

Inner class means one class that is a member of another class. There are basically four types of inner classes in java. Nested Inner class can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and default modifier.

Shadowing in Java is the practice of using variables in overlapping scopes with the same name where the variable in low-level scope overrides the variable of high-level scope. Here the variable at high-level scope is shadowed by the low-level scope variable. Basic knowledge of this keyword is required before moving ahead.

Implementation:

Here we will be discussing a few examples to get a better understanding of the concept as we will be able to perceive it better alongside code and later will discuss how the output is generated.

Example 1 

Java




// Java program to Demonstrates Shadowing in Java
 
// Class 1 and 2
// Outer Class
class Shadowing {
 
    // Custom instance variable or member variable
    String name = "Outer John";
 
    // Nested inner class
    class innerShadowing {
 
        // Instance variable or member variable
        String name = "Inner John";
 
        // Method of this class to
        // print content of instance variable
        public void print()
        {
 
            // Print statements
            System.out.println(name);
            System.out.println(Shadowing.this.name);
        }
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Accessing an inner class by
        // creating object of outer class inside main()
        Shadowing obj = new Shadowing();
 
        Shadowing.innerShadowing innerObj
            = obj.new innerShadowing();
 
        // Calling method defined inside inner class
        // inside main() method
        innerObj.print();
    }
}


Output

Inner John
Outer John

 Output explanation: 

In this example, you can see that name is declared as String variable inside Shadowing class as well as innerShadowing class. When we print just name then it prints the value of name stored at innerShadowing class because the scope of this class is less than outer class so it overrides the value of the name.

Let’s have a look at another example that will clarify the concept more clearly as follows:

Example 2

Java




// Java program to demonstrates Shadowing in Java
 
// Class 1 and 2
// Outer Class
class Shadowing {
 
    // Instance variable or member variable
    String name = "Outer John";
 
    // Nested class
    // Inner Class
    class innerShadowing {
 
        // Instance variable or member variable
        String name = "Inner John";
 
        // Method of inner class
        // To print the content
        public void print(String name)
        {
 
            // Print statements
 
            System.out.println(name);
            // This keyword refers to current instance
            // itself
            System.out.println(this.name);
            System.out.println(Shadowing.this.name);
        }
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Accessing an inner class by
        // creating object of Outer class inside main()
        // method
        Shadowing obj = new Shadowing();
 
        Shadowing.innerShadowing innerObj
            = obj.new innerShadowing();
 
        // Function Call
        innerObj.print("Parameter John");
    }
}


Output

Parameter John
Inner John
Outer John

Output explanation:

In this example, we pass the argument to the print() method. So we can see now for printing the name of the inner class we need to use ‘this’ because the scope of the print() method is less than that of inner class so it overrides the name of the inner class too.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments