Lec 25 - Subtyping and Dynamic Dispatch

Void decincby y dynamic dispatch finding the code

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Table Decr! Decr d = _;! d.dec();! int x = d.get();! Object! x! initY! 2! String toString(){… ! y! this! 0! 2! boolean equals…! …! Counter! extends Object ! Done with the call to the Decr constructor, so pop the stack and return to the saved workspace, returning the newly allocated object (now in the this pointer). Counter() { x = 0; }! void incBy(int d){…}! int get() {return x;}! Decr! extends Counter ! Decr(int initY) { … }! void dec(){incBy(-y);}! Returning the Newly Constructed Object Workspace Stack Decr d = ;! d.dec();! int x = d.get();! Heap Class Table Decr! Object! x! 0! String toString(){… ! y! 2! boolean equals…! …! Counter! extends Object ! Concnue execucng the program. Counter() { x = 0; }! void incBy(int d){…}! int get() {return x;}! Decr! extends Counter ! Decr(int initY) { … }! void dec(){incBy(-y);}! Allocacng a local variable Workspace d.dec();! int x = d.get();! Stack d! Heap Class Table Decr! Object! x! 0! String toString(){… ! y! 2! boolean equals…! …! Counter! extends Object ! Allocate a stack slot for the local variable d. It’s mutable… (see the bold box in the diagram). Aside: since, by default, fields and local variables are mutable, we oien omit the bold boxes and just assume the contents can be modified. Counter() { x = 0; }! void incBy(int d){…}! int get() {return x;}! Decr! extends Counter ! Decr(int initY) { … }! void dec(){incBy(-y);}! Dynamic Dispatch: Finding the Code Workspace .dec();! int x = d.get();! Stack d! Heap Class Table Decr! Object! x! 0! String toString(){… ! y! 2! boolean equals…! …! Counter! extends Object ! Counter() { x = 0; }! Invoke the dec method on the object. The code can be found by “pointer chasing”. This process is called dynamic...
View Full Document

Ask a homework question - tutors are online