Introduction to OOP Chapter 16: Overriding : next previous audio real text

Shadowing of Instance Variables in Java

Java allows instance variables to be redefined, and uses shadowing.
class Parent {
	public int x = 12;
}

class Child extend Parent {
	public int x = 42; // shadows variable from parent class
}

	Parent p = new Parent();
	System.out.println(p.x);
12
	Child c = new Child();
	System.out.println(c.x);
42
	p = c; // be careful here!
	System.out.println(p.x);
12
Intro OOP, Chapter 16, Slide 19