Defined OOL, School, Java
- Dynamic method dispatch vs static method dispatch.
The following program in DOOL (Defined OOL) (6.3, p203-204), has a method m2 in c1 that calls self on a method m1, which is both in c1 and c2.Is the m1 that gets called in the same class as m2 (static dispatch) or in the same class as self (dynamic dispatch).
What is the value ofclass c1 extends object method initialize () 1 method m1 () 1 method m2 () send self m1() class c2 extends c1 method m1 () 2 let o1 = new c1() o2 = new c2() in list(send o1 m1(), send o2 m1(), send o2 m2())send o2 m2()(using interp6-3ool.scm? Dynamic or static dispatch?
- Translating from DOOL to School:
What is the output using school.scm or the revised version schoolnew.scm? Is dispatch dynamic or static?(define-class (c1 () () (baseobj)) () () (cinit +) () ((m1 (lambda () 1)) (m2 (lambda () (ask this m1)))) (init +)) (define-class (c2 () () ((c1))) () () (cinit +) () ((m1 (lambda () 2))) (init +)) (let ((o1 ((c1))) (o2 ((c2)))) (writeln (ask o1 m1) " " (ask o2 m1) " " (ask o2 m2)))
- Translating into Java.
What is the output? Are methods dynamically or statically dispatched?import java.io.*; class C1 { int initialize() { System.out.println("initialize called"); return 1; } int m1() { return 1; } int m2() { return this.m1(); } public C1() { initialize(); System.out.println("C2 instance created"); } } class C2 extends C1 { int m1() { return 2; } public C2() { super(); System.out.println("C1 instance created"); } } public class P203 { public static void main(String[] args) { C1 o1 = new C1(); C2 o2 = new C2(); System.out.println(o1.m1()); System.out.println(o2.m1()); System.out.println(o2.m2()); } }- The DOOL program on EOPL II 6.4, p224 is to be used to test interp6.3ool.scm for assignment 8. Think through what the output should be.
These are a couple examples of how a program an be translated from one OOL to another. The behavior of a program is determined by the semantics of the language.
- The program in School is ( p224school.scm):
;;; p224school.scm ;;; The program on page 224 in School (define-class (a () () (baseobj)) () () (cinit +) ((i 0) (j 0)) ((setup (lambda () (set! i 15) (set! j 20) 50)) (geti (lambda () i)) (f (lambda () (ask this g))) (g (lambda () (+ i j)))) (init +)) (define-class (b () () ((a))) () () (cinit +) ((j 0) (k 0)) ((setup (lambda () (set! j 100) (set! k 200) (ask super setup) (ask this h))) (getj (lambda () j)) (getk (lambda () k)) (g (lambda () (list (ask this geti) j k))) ; need geti (h (lambda () (ask super g)))) (init +)) (define-class (c () () ((b))) () () (cinit +) () ((g (lambda () (ask super h))) (h (lambda () (+ (ask this getk) (ask this getj))))) (init +)) ;; (ask ((c)) setup) crashes since there is a problem with super ;; schoolnew.scm needs to be fixed before removing the ';' (let ((p (lambda (o) (let ((u (ask o setup))) (list u (ask o g) (ask o f)))))) (list (p ((a))) (p ((b))) ; (p ((c))) ))
- Translating to Java, we need to use a data type that behaves like lists -- the elements of the data type can be other members of the data type.
A
Vectorcan have any Object as a member. But it can't have primitive types as members. Integers can be represented as objects using theInteger"wrapper" class.
- Translation into java: P224.java
import java.io.*; import java.util.*; class A { int i; int j; public A() { System.out.println("A instance is created"); initialize(); } void initialize () { System.out.println("A is initialized"); } Vector setup() { i = 15; j = 20; Vector v = new Vector(); v.add(new Integer(50)); return v; } Vector f() { return this.g(); } Vector g() { Vector v = new Vector(); v.add(new Integer(i + j)); return v; } } class B extends A { int j; int k; public B() { super(); System.out.println("B instance is created"); } Vector setup() { j = 100; k = 200; super.setup(); return this.h(); } Vector g() { Vector v = new Vector(); v.add(new Integer(i)); v.add(new Integer(j)); v.add(new Integer(k)); return v; } Vector h() { return super.g(); } } class C extends B { public C() { System.out.println("C instance is created"); } Vector g() { return super.h(); } Vector h() { Vector v = new Vector(); v.add(new Integer(k + j)); return v; } } public class P224 { static Vector p(A o) { Vector u = o.setup(); Vector v = o.g(); Vector w = o.f(); Vector z = new Vector(); z.add(u); z.add(v); z.add(w); return z; } public static void main(String[] args) { Vector x = p(new A()); Vector y = p(new B()); Vector z = p(new C()); Vector w = new Vector(); w.add(x); w.add(y); w.add(z); System.out.println(x); System.out.println(y); System.out.println(z); System.out.println(w); } }DOOL and Java have dynamically dispatched methods. School has statically dispatched methods.
What is the case with C++? (What does
virtualdo?)