Lecture 17 Dynamic method dispatch e.g., class Oval { public Oval() { } public String getShape () { return "Oval"; } } class Circle extends Oval { public Circle() { } public String getShape() { return "Circle"; } } class test { static void printShape(Oval x) { System.out.println (x.getShape()); } public static void main (String [] args) { Oval x; if (args[0].equals("circle") { x = new Circle(); } else { x= new Oval(); } printShape (x); // which class x is cannot be determined at complie time } } what is polymorphism? what is overloading? what is overriding? To implement dynamic method binding Oval vtable _______ ___________ | | -----> | getShape | ---> |------| |----------| | | | | | | | | | | | | ------- ----------- r1 := f r2 := *r1 r2 := * (r2 + (3-1) *4) call *r2 ^ ^ | |________________ each entry occupies 4 bytes | 3rd entry in vtable multiple inheritance A B ^ ^ \ / \ / \ / \/ C class Dog { color Hairness print() { } } class Guard { HoursOnDuty print () { } } class GuardDog : public Guard, public Dog { } x = new GuardDog () x.print(); // which print() to call? class GuardDog : public Guard, public Dog { Dog::print (); // in C++ we can specify which print() to inherit } class Guard { virtual print() { } } class Dog { virtual print() { } } class GuardDog { print() { ....} } Offset of Dog view is known at compile time GuardDog -----> |---------| Guard | |-----> vtable (of Guard) view |---------| | | | | |---------| -----> | |-----> vtable (of Dog) Dog |---------| view | | |---------| |guarddog | | data | |---------| Shared multiple inheritance (Figure 10.9 at page 573) "mix-in" inheritance: (Java)