CS 251 Fall 2019 Principles of Programming Languages
Ben Wood
λ
CS 251 Fall 2019
Principles of Programming Languages
Ben Wood
λ
https://cs.wellesley.edu/~cs251/f19/
Dynamic Dispatch
semantic essence
- f "object-oriented" programming languages
(OOP)
1 Dynamic Dispatch
How are names resolved?
Key piece of semantics in any language.
- ML, Racket:
– Just one kind of variables. – Lexical scope – unambiguous binding – Record field names are not variables: no "lookup"
- Java, …:
– Local variables: lexical scope (more limited) – Instance variables, methods
- Look up in terms of special self / this "variable"
- it's more complicated…
Dynamic Dispatch 2
Method lookup in OO languages
Two key questions for Java:
– General case: What m is run by ___.m() ? – Specific case: What m is run by this.m() ?
3 Dynamic Dispatch Dynamic Dispatch 4
class Point { double x, y; Point(double x, double y) { this.x = x; this.y = y; } double getX() { return this.x; } double getY() { return y; } double distFromOrigin() { return Math.sqrt(this.getX() * this.getX() + getY() * getY()); } } class PolarPoint extends Point { // poor design, useful example double r, theta; PolarPoint(double r, double theta) { super(0.0, 0.0); this.r = r; this.theta = theta; } double getX() { return this.r * Math.cos(this.theta); } double getY() { return r * Math.sin(theta); } }
Point p = …; // ??? p.getX(); // ??? p.distFromOrigin(); // ???
- v
- verriding
im implicit icit this.
dy dynamic di disp spatch