Overriding Variables: Shadowing We can override methods, can we override instance variables too? Answer: Yes, it is possible, but not recommended Overriding an instance variable is called shadowing, because it makes the base instance variables of the base class inaccessible. (We can still access it explicitly using super.varname). public class Person { public class Staff extends Person { String name; String name; // // name refers to Staff s name This can be confusing to readers, since they may not have noticed that you redefined name. Better to just pick a new variable name 30
Shadowing example class Base { public Base(){x = 10; public String foo(){return x+""; class Derived extends Base { public Derived(){ x = 20; public String foo(){return (x + "\t" + super.x); Derived d = new Derived(); d.foo(); 31
Shadowing example class Base { public Base(){x = 10; public String foo(){return x+""; class Derived extends Base { public Derived(){ x = 20; public String foo(){return (x + "\t" + super.x); Derived d = new Derived(); d.foo(); 20 10 32
Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; b.foo(); 33
Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; b.foo(); 20 10 34
Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; d.x; b.x; 35
Shadowing example class Base { public Base(){x = 10; public void foo(){return x); class Derived extends Base { public Derived(){ x = 20; public void foo(){return (x + \t + super.x); Derived d = new Derived(); Base b = d; d.x; 20 b.x; 10 36
super and this super: refers to the base class object We can invoke any base class constructor using super( ). We can access data and methods in the base class (Person) through super. E.g., tostring( ) and equals( ) invoke the corresponding methods from the Person base class, using super.tostring( ) and super.equals( ). this: refers to the current object We can refer to our own data and methods using this. but this usually is not needed We can invoke any of our own constructors using this( ). As with the super constructor, this can only be done within a constructor, and must be the first statement of the constructor. Example: public Fraction(int n) { this(n,1); 37
Memory Layout class Base{ private int a; protected int b; protected int c; protected void m1(){ public void m2(){ Base class Child extends Base{ private int d; public void m1(){ public void m3(){ Child The Java Virtual Machine does not mandate any particular internal structure for objects. 38
Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ Base object Pointer to vtable a b c class Child extends Base{ private int d; public void m1(){ public void m3(){ 39
Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ Base object Pointer to vtable a b c class Child extends Base{ private int d; public void m1(){ public void m3(){ VTABLE Pointer to m1() Pointer to m2() Pointer to m3() Child object Pointer to vtable a b c d 40
Memory Layout VTABLE class Base{ private int a; Pointer to m1() protected int b; Pointer to m2() protected int c; protected void m1(){ public void m2(){ class Child extends Base{ private int d; public void m1(){ public void m3(){ Each class has one vtable. All objects of the this class shares the vtable. VTABLE Pointer to m1() Pointer to m2() Pointer to m3() Base object Pointer to vtable a b c Child object Pointer to vtable a b c d 41
Inheritance and Private Private members: Child class inherits all the private data of Base class However, private members of the base class cannot be accessed directly Why is this? After you have gone to all the work of setting up privacy, it wouldn t be fair to allow someone to simply extend your class and now have access to all the private information 42
Quiz 5: True/False Excepting Object, which has no superclass, every class has one and only one direct superclass. A. True B. False 43
Quiz5: True/False Excepting Object, which has no superclass, every class has one and only one direct superclass. A. True B. False 44
Quiz 6: class Base { public void foo(){ println("base"); class Derived extends Base { private void foo(){ println("derived"); Base b = new Derived(); b.foo(); A. Base B. Derived C. Compiler Error D. Runtime Error 45
Quiz 6: class Base { public void foo(){ println("base"); class Derived extends Base { private void foo(){ println("derived"); Base b = new Derived(); b.foo(); A. Base B. Derived C. Compiler Error D. Runtime Error It is compiler error to give more restrictive access to a derived class function which overrides a base class function. 46
Quiz 7: class Animal has a subclass Mammal. Which of the following is true: A. Because of single inheritance, Mammal can have no subclasses. B. Because of single inheritance, Mammal can have no other parent than Animal. C. Because of single inheritance, Animal can have only one subclass. D. Because of single inheritance, Mammal can have no siblings. 47
Quiz 7: class Animal has a subclass Mammal. Which of the following is true: A. Because of single inheritance, Mammal can have no subclasses. B. Because of single inheritance, Mammal can have no other parent than Animal. C. Because of single inheritance, Animal can have only one subclass. D. Because of single inheritance, Mammal can have no siblings. 48
Access level Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N 49