| Introduction to OOP | Chapter 16: Overriding : | next | previous | audio | real | text |
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