Inheritance - Part 1

Variable Shadowing

Shadowing in Java is a concept rooted in Object-Oriented Programming (OOP) principles. It enables a subclass to introduce a specific implementation for a member variable or method that already exists in its superclass. Essentially, shadowing and hiding are akin concepts applied in different contexts, occurring during compile time. In this section, we delve into variable and method shadowing in Java, elucidating the concept with examples.
Understanding Shadowing in OOP
Shadowing occurs when a variable in a narrower scope (like a decision block, method, or inner class) shares the same name as a variable in an enclosing scope. In such cases, the inner declaration "shadows" the outer one.
In simpler terms, shadowing allows for a new implementation of a member in a derived class that hides the original implementation present in the base class. This mechanism permits the derived class to mask the implementation of a method in the base class and to call the base class's method implementation through an object of the derived class.
The crucial point to note is that the original (shadowed) declaration becomes inaccessible directly by name. The term "shadowing" is akin to "masking," hence it's also known as name masking.
Variable Shadowing and Hiding in Java
Java supports three types of variables: local variables, instance variables, and class variables (static variables). Shadowing typically occurs between local and instance variables. When a local variable shares the same name as an instance variable, referencing that name within the method's scope will point to the local variable, effectively shadowing the instance variable.
Example Demonstrations
Example 1: Basic Shadowing
Consider a class Car where both instance and local variables share the same names but are intended to represent different data:
public class Vehicle {

private String name = "Car";




public void printName() {

String name = "Truck";

System.out.println("Local Name: " + name);

System.out.println("Instance Name: " + this.name);

}




public static void main(String[] args) {

Vehicle vehicle = new Vehicle();

vehicle.printName();

}

}
Output:
Local Name: Truck

Instance Name: Car
In the example, the printName method declares a local variable name, which shadows the instance variable name declared in the class. Inside the method, when we refer to a name, it refers to the local variable. To access the instance variable, we use the this keyword as demonstrated.
Example 2: Shadowing with Inner Classes
Here's another example demonstrating shadowing within the context of an inner class:
public class Company {

private String name = "ABC Corporation";




class Department {

private String name = "Human Resources";




void printDepartmentName(String name) {

System.out.println("Input name = " + name);

System.out.println("Department name = " + this.name);

System.out.println("Company name = " + Company.this.name);

}

}



public static void main(String[] args) {

Company company = new Company();

Company.Department department = company.new Department();

department.printDepartmentName("Finance");

}

}
Output:
Input name = Finance

Department name = Human Resources

Company name = ABC Corporation
In this example, the Department class has a private instance variable name, which shadows the name variable of the outer class Company. The printDepartmentName method takes a name parameter, which shadows the name variable of the Department class. Inside the method, when we refer to a name, it refers to the parameter name. To access the instance variable of the Department class, we use “this dot name,” and to access the instance variable of the Company class, we use “Company dot this dot name.”
These examples clarify how shadowing functions in Java allow variables in an inner scope to reuse names from an outer scope but refer to different entities. This feature necessitates careful attention to detail in code involving multiple layers of scope.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.