Virtual Table for classes Wall and Ball

This slide illustrates the virtual method tables for classes Wall and Ball. Both Ball and Wall inherit their first three methods from their parent class, Graphical Object. The class Wall also inherits the update method, while the class Ball choses to override this method. Both classes override the draw and hitby methods. Finally, each class adds some new methods to those inherited from their parent.

By indexing into the virtual method table by a fixed offset, for example indexing into location 4 to find the update method, the virtual method dispatch code will locate the correct method to execute, whatever the type of value a polymorphic variable is actually holding.

The overhead involved in a message disatch can therefore be seem to be simply one level of pointer indirection, and an array lookup by a fixed offset. This is genrally no more than one or two assembly language instructions.

[audio] [real] Text to accompany slide8, in Chapter 21 of An Introduction to Object-Oriented Programming