{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec 25 - Subtyping and Dynamic Dispatch

Void decincby y dynamic dispatch finding the code

Info icon This 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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern