Hands-on practice for this lecture. Work through the exercises and quizzes to reinforce what you've learned.
Exercise 1 of 1
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]
}
}[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.