Shadowing & Hiding

Hands-on practice for this lecture. Work through the exercises and quizzes to reinforce what you've learned.

1

Exercise 1 of 1

Shadowing vs Hiding: The Reference Lab

Names can be confusing when they overlap. Learn to distinguish between local variables, instance attributes, and parent attributes.

class Parent {
    String name = "Parent";
}

class Child extends Parent {
    String name = "Child";

    void printNames() {
        String name = "Local";
        System.out.println(name);        // [1]
        System.out.println(this.name);   // [2]
        System.out.println(super.name);  // [3]
    }
}

Predict the Output

[1] System.out.println(name)

[2] System.out.println(this.name)

[3] System.out.println(super.name)

🌘 Variable Hiding: Unlike methods, attributes are not polymorphic. They are "hidden" by the child. You must use this or super to be specific.

Practice: Shadowing & Hiding — Interactive Exercises | Durgesh Rai