Lecture 20

  1. Runtime Object Allocation

1.0 - Runtime Object Allocation

1.1 - Sub_Classing

class S extends T

1.1.1 - Sub-Classing Example

public class A {
	int i;
	A() { i = 0; }
	int m(int x) { i = x; return x+1; }
	int getI() { return i; }
}

public class B extends A {
	// inherits field i
	int j;
	B{} ( super(); j = 1; )
	// overrides the definition of A.m in the local scope 
	int m(int x) { return super.m(x) + j; }
	// defines a new method
	int getJ() { return j; }
}

public class C extends B {
	// inherits field i (from A)
  // inherits field j (from B)
	C() { super(); }
	// inherits method getI (from A)
	// inherits method getJ (from B)
	// overrides the definition of B.m in the local scope 
	int m(int x) { return super.m(x) - j }
}

1.2 - Dynamic Dispatch

class S extends T

1.2.1 - Dynamic Dispatch Table

1.3 - This, or Self

class T {
	...
	void m(int n) {      // Really calling void m(T this, int n)
		...
	}
	...
}

1.4 - Super

class S extends T

1.4.1 - Superclass Example

public class A {
	int i;
	A() { i = 0; }
	int m(int x) { i = x; return x+1; }
	int getI() { return i; }
}

public class B extends A {
	// inherits field i
	int j;
	B{} ( super(); j = 1; ) // Invoke A's constructor
	// overrides the definition of A.m in the local scope 
	int m(int x) { return super.m(x) + j; } //  call A.m
	// defines a new method
	int getJ() { return j; }
}

public class C extends B {
	// inherits field i (from A)
  // inherits field j (from B)
	C() { super(); } // Invoke B's constructor
	// inherits method getI (from A)
	// inherits method getJ (from B)
	// overrides the definition of B.m in the local scope 
	int m(int x) { return super.m(x) - j }
}g

1.5 - Instance Of

x instanceof T
A p = new A();
A q = new B();
B r = new C();
Variable X x instanceof A x instanceof B x instanceof C
p true false false
q true true false
r true true true
null false false false
A p = new A(); // Static type A, Dynamic type A
A q = new B(); // Static type A, Dynamic type B
B r = new C(); // Static type B, Dynamic type C


1.6 - Static Fields and Methods

1.7 - Interfaces