# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-book fa-stack-1x fa-inverse"></i></span> Delegation-based Languages - Create objects rather than instantiate classes - Use delegation instead of inheritance - Example: Javascript <div class="grid grid-cols-3 gap-4"> <div class="col-span-2"> ```js var A = { f (x) { console.log ("A.f (" + x + ")") return (x == 0) ? this.g () : this.f (x - 1); }, g () { console.log ("A.g ()"); return 0; } } var B = { f (x) { console.log ("B.f (" + x + ")"); return super.f (x); }, g () { console.log ("B.g ()"); return 0; } } ``` </div> <div> - Link objects ```js Object.setPrototypeOf(B, A); B.f (2); ``` * Output at runtime ```sh B.f (2) A.f (0) A.f (2) B.g () B.f (1) $1 ==> 0 A.f (1) B.f (0) ``` </div> </div> ---
# <span class="fa-stack"><i class="fa-solid fa-circle fa-stack-2x"></i><i class="fa-solid fa-book fa-stack-1x fa-inverse"></i></span> Static and Dynamic Type With Lambda Notation - :fa fa-question-circle: What is the type of `fs[i]`? Which `apply` method? ```java interface Fn { int apply (int x); } ``` ```java Fn[] fs = new Fn[3]; for (int i = 0; i < fs.length; i++) { int j = i + 1; // effectively final fs[i] = x -> x + j; } for (int i = 0; i < fs.length; i++) { System.out.println(i + "=" + fs[i].apply(20)); } // prints 0=21, 1=22, 2=23 ``` ---