i219 Software Design Methodology
5. Object‐oriented
programming language 2
Kazuhiro Ogata (JAIST)

2

Outline of lecture• Method dispatch• Inner interface/class• Enum type

3Method dispatch (1)For a while, let us only consider methods that have no arguments.
Under the condition, let us consider the class diagram:
C0C1

Cn–1

Cn

where C0 is Object

When a message m( ) is sent to an object an of Cn, the search forthe method invoked basically starts with Cn; if there exists i (0 ≦ i≦ n) such that Ci has the method m( ), and each Ck (i+1 ≦ k ≦n) does not have such a method, then the method in Ci is invoked.
Note that executing o.m( ) where o is a variable of Ci (i ≠ 1) but refers to an
object of Cn, the search starts with Cn.
Note that compiling o.m( ) fails if o is a variable of Ci such that C0,…, Ci do nothave m( ); type casting may make the compilation successful.

4

Method dispatch (2)(new C4()).m1();

m1() in C2 is invoked.

(new C4()).m2();

m2() in C1 is invoked.

(new C4()).m3();

m3() in C4 is invoked.

C1 o = new C4();o.m1();

m1() in C2 is invoked.

o.m2();

m2() in C1 is invoked.

o.m3();

A compiler error occurs.

5Method dispatch (3)There are two pseudo‐variables that refer to the currently active
object: this and super.
Let both this and super refer to an object of Cn.
If a message m() is sent to this, the search for the method invoked
starts with Cn as the message is sent to an object of Cn.
If a message m() is sent to super, the search for the method
invoked starts with the super class of the class in which super.m()appears.

6

Method dispatch (4)(new C6()).m1();

m1() in C5 is invoked.

m1() in C5 is as follows:
public void m1() {System.out.println("m1() in C5 was invoked.");m2() in C6 is invoked.
this.m2();
m2() in C1 is invoked.
super.m2();}

7

Method dispatch (5)A sequence τ1, …, τm of types is compatible with a parameter t1 p1,…, tn pn if m = n and for each i (1 ≦ i ≦ n) τi is the same as ti or asubtype of ti.
E.g., C4, C4 is compatible with C3 x, C4 y.
t1 p1, …, tn pn ≦ t'1 p'1, …, t'm' p'm' if m' = n and for each i (1 ≦ i ≦n) ti is the same as t'i or a subtype of t'i.
E.g., x:C3, y:C4 ≦ x:C2, y:C3.
Note that if τ1, …, τm is compatible with t1 p1, …, tn pn and t1 p1,…, tn pn ≦ t'1 p'1, …, t'm' p'm' then τ1, …, τm is compatible witht'1 p'1, …, t'm' p'm'E.g., C4, C4 is also compatible with x:C2, y:C3.

8Method dispatch (6)Let P be a set of parameters with which τ1, …, τm is compatible.
P ∈ P is called the minimum (or least) element of P if P ≦ P' for
all P' ∈ P.
E.g., {(C3 x, C4 y), (C2 x, C3 y), (C3 x, C2 y), (C2 x, C2 y), (C1 x,C2 y)} is a set of parameters with which C4, C4 is compatible. C3x, C4 y is the minimum element of the set.
E.g., {(C2 x, C2 y), (C1 x, C2 y)} is a set of parameters with which
C2, C2 is compatible. C2 x, C2 y is the minimum element of theset.
E.g., {(C2 x, C3 y), (C3 x, C2 y), (C2 x, C2 y), (C1 x, C2 y)} is a setof parameters with which C3, C3 is compatible. The set does nothave the minimum element .

9Method dispatch (6)Let us consider obj.m(v1, …, vm) that appears in Ck.
For each vj, let tj be its type that can be known at compile timeand Cobj be the class of obj that can be known at compile time.
E.g., if vj (or obj) is new C2(), tj (or Cobj) is C2, if vj (or obj) is x thatis a variable declared as C1 x, then tj (or Cobj) is C1 even though anobject of C2 is stored in x, if vj (or obj) is the pseudo‐variable this,tj (or Cobj) is Ck, and if vj is 3.14, tj is double.
If obj is the pseudo‐variable super, Cobj is Ck–1 (the super class ofCk). Note that super cannot be used as an actual parameter of amethod, such as vj.

10Method dispatch (6)Let M be the set of methods in C0, …, Cobj such that their names
are the same as m, and P be {P | m(P) ∈ M, t1, …, tm iscompatible with P}.
If P has the minimum element Pmin, then the method signaturem(Pmin) is bound to the message passing statement obj.m(v1, …, vm)
at compile time. Otherwise, an error occurs.
The method dispatch for obj.m(v1, …, vm) at runtime is done in
the same way as that for obj.m() where m has no parameters. Themethod signature of the method invoked for the latter case is m(),while that for the former is m(Pmin).

11

Method dispatch (7)(new C8()).m(new C4(),new C4());

m(C3 x,C4 y) in C7 is invoked.
C2 x = new C4();
C2 y = new C4();(new C8()).m(x,y);m(C2 x,C2 y) in C7 is invoked.

(new C8()).m(new C3(),new C3());

A compiler error occurs.

12

Method dispatch (8)(new C10()).m();m() in C10 is invoked.

m1() in C10 is as follows:
public void m() {System.out.println("m() in C10 was invoked.");m(C3 x,C4 y) in C10 is invoked.
this.m(new C4(),new C4());
m(C3 x,C4 y) in C7 is invoked.
super.m(new C4(),new C4());
m(C2 x) in C10 is invoked.
this.m(new C3());// super.m(new C3()); // if uncommented, javac complains.
}13

Method dispatch (9)if this refers to an object of SubA, A & SuperA,
then m2() in SubA, A & SuperA are invoked, respectively

public void m1() { System.out.print("m1() in SuperA;"); this.m2(); }public void m2() { System.out.println("m2() in SuperA;"); }public void m2() { System.out.print("m2() in A;"); super.m2(); }m2() in SuperA is invoked

public void m2() { System.out.print("m2() in SubA;"); super.m2(); }m2() in A is invoked

14

Method dispatch (10)public void m1() { System.out.print("m1() in SuperA;"); this.m2(); }public void m2() { System.out.println("m2() in SuperA;"); }public void m2() { System.out.print("m2() in A;"); super.m2(); }public void m2() { System.out.print("m2() in SubA;"); super.m2(); }main(…) in A is as follows:
public static void main(String[] args) { (new A()).m1(); }m1() in SuperA;m2() in A;m2() in SuperA;

15

Method dispatch (11)public void m1() { System.out.print("m1() in SuperA;"); this.m2(); }public void m2() { System.out.println("m2() in SuperA;"); }public void m2() { System.out.print("m2() in A;"); super.m2(); }public void m2() { System.out.print("m2() in SubA;"); super.m2(); }main(…) in SubA is as follows:
public static void main(String[] args) { (new SubA()).m1(); }m1() in SuperA;m2() in SubA;m2() in A;m2() in SuperA;

16

Method dispatch (12)SuperB

17

Method dispatch (13)In SSuperB:m2(SuperB x,SuperB y){ …println("m2(SuperB x,SuperB y) in SSuperB;"); }m2(SSuperB x,SSuperB y){ …println("m2(SSuperB x,SSuperB y) in SSuperB;"); }In SuperB:
m1(SuperB x) { …print("m1(SuperB x) in SuperB;"); AMsgPassing }m2(SubB x,SubB y) { …println("m2(SubB x,SubB y) in SuperB;"); }m2(SuperB x,SuperB y){ …println("m2(SuperB x,SuperB y) in SuperB;"); }18

Method dispatch (14)In B:
m2(SuperB x,SuperB y) { …print("m2(SuperB x,SuperB y) in B;");super.m2(new B(),new B()); }m2(SubB x,SubB y) { …print("m2(SubB x,SubB y) in B;");super.m2(new B(),new B()); }m2(B x,B y) { …print("m2(B x,B y) in B;"); super.m2(new B(),new B()); }main(String[] args) { (new B()).m1(new B()); }In SubB:m2(SuperB x,SuperB y) { …print("m2(SuperB x,SuperB y) in SubB;");super.m2(x,y); }m2(SubB x,SubB y) { …print("m2(SubB x,SubB y) in SubB;");super.m2(x,y); }m2(B x,B y) { …print("m2(B x,B y) in SubB;");super.m2(x,y); }main(String[] args) { (new SubB()).m1(new B()); }19

Method dispatch (15)
1. AMsgPassing is (new SubB()).m2(new B(),new B());% java B
m1(SuperB x) in SuperB;m2(B x,B y) in SubB;m2(B x,B y) in B;m2(SuperBx,SuperB y) in SuperB;% java SubB
m1(SuperB x) in SuperB;m2(B x,B y) in SubB;m2(B x,B y) in B;m2(SuperBx,SuperB y) in SuperB;

2. AMsgPassing is this.m2(new B(),new B());% java B
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in B;m2(SuperB x,SuperB y) inSuperB;% java SubB
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in SubB;m2(SuperB x,SuperB y)
in B;m2(SuperB x,SuperB y) in SuperB;

20

Method dispatch (16)
3. AMsgPassing is this.m2(new SubB(),new SubB());% java B
m1(SuperB x) in SuperB;m2(SubB x,SubB y) in B;m2(SuperB x,SuperB y) inSuperB;% java SubB
m1(SuperB x) in SuperB;m2(SubB x,SubB y) in SubB;m2(SubB x,SubB y) in
B;m2(SuperB x,SuperB y) in SuperB;

4. AMsgPassing is this.m2(this,this);% java B
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in B;m2(SuperB x,SuperB y) inSuperB;% java SubB
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in SubB;m2(SuperB x,SuperB y)
in B;m2(SuperB x,SuperB y) in SuperB;

21

Method dispatch (17)5. AMsgPassing is SuperB o = new SubB(); o.m2(new B(),new B());% java B
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in SubB;m2(SuperB x,SuperB y)
in B;m2(SuperB x,SuperB y) in SuperB;% java SubB
m1(SuperB x) in SuperB;m2(SuperB x,SuperB y) in SubB;m2(SuperB x,SuperB y)
in B;m2(SuperB x,SuperB y) in SuperB;

6. AMsgPassing is SuperB o1 = new SubB(); o1.m2(new SSuperB(),new B());% java B
m1(SuperB x) in SuperB;m2(SSuperB x,SSuperB y) in SSuperB;% java SubB
m1(SuperB x) in SuperB;m2(SSuperB x,SSuperB y) in SSuperB;

22

Method dispatch (18)7. AMsgPassing is this.m2(new SSuperB(),new B());% java B
m1(SuperB x) in SuperB;m2(SSuperB x,SSuperB y) in SSuperB;% java SubB
m1(SuperB x) in SuperB;m2(SSuperB x,SSuperB y) in SSuperB;

8. AMsgPassing is this.m2(new Object(),new B());
javac complains.

23

Inner class/interface
Classes & interfaces can be declared inside a class; called innerclasses & interfaces.
public class NestedClass {an inner interface
private interface MyInterface { String m(); }private class MyClass implements MyInterface
an inner class{ public MyClass() {}public String m() { return "MyClass"; } }private MyClass o = new MyClass();
public String m() { return "I have " + o.m() + "."; } }Compiling NestedClass (NestedClass.java), in addition toNestedClass.class, NestedClass$MyInterface.class &NestedClass$MyClass.class are made for the inner interface & class.

24Enum type (1)An enum type is a special class of Java.
public enum TokenName { PLUS, MUL, NUM, UNDEF, }instances of TokenName; (called enum constants)
no other instances of TokenName

An enum constant EC in an enum type ET is refered as ET.EC.
public String toString() throws IllegalStateException {if (this.name == TokenName.PLUS) { return "plus"; }else if (this.name == TokenName.MUL) { return "mul"; }else if (this.name == TokenName.NUM) { return "num=" + num; }else if (this.name == TokenName.UNDEF) { return "undef:" + undef; }else { throw new IllegalStateException("in Token1!"); } } }checks if all cases are covered at runtime

25

Enum type (2)When an enum type ET is used in switch, however, an enum
constant EC should be referred as EC instaed of ET.EC.
public String toString() throws IllegalStateException {switch(this.name) {case PLUS: return "plus";
case MUL: return "mul";case NUM: return "num=" + num;case UNDEF: return "undef:" + undef;
default: throw new IllegalStateException("in Token2!"); } }Using an enum type in switch, an inner class is made by javac.
This is why Token2$1.class is generated when compiling
Token2.java.

26

Summary• Method dispatch• Inner interface/class• Enum type