Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Dispatch in the Java Virtual Machine TM What is (dynamic) multi-dispatch? Method selection based on the types of … all (or more than one) of the.

Similar presentations


Presentation on theme: "Multi-Dispatch in the Java Virtual Machine TM What is (dynamic) multi-dispatch? Method selection based on the types of … all (or more than one) of the."— Presentation transcript:

1 Multi-Dispatch in the Java Virtual Machine TM What is (dynamic) multi-dispatch? Method selection based on the types of … all (or more than one) of the arguments … at execution time (not compile time). Theatre t = new Theatre(); Orchestra o = new Orchestra(); Person duane = new Person(" Duane "); Person chris = new Student(" Chris "); System.out.println(duane + ": " + t.admission(duane) + " and " + o.admission(duane) + "." ); System.out.println(chris + ": " + t.admission(chris) + " and " + o.admission(chris) + "." ); Duane: pays $10 and pays $40. Chris: pays $10 and pays $40. Duane: pays $10 and pays $40. Chris: pays $5 and pays $25. class Person { /* … */ } class Student extends Person { /* … */ } class Theatre { String admission(Person p) {return "pays $10" ;} String admission(Student s) {return "pays $5" ; }} class Orchestra { String admission(Person p) {return "pays $40" ;} String admission(Student s) {return "pays $25" ;}} What does this code return? Given these definitions: Isn’t this method overloading? Statically, chris is a ‘Person’. Dynamically, chris is a ‘Student’. The Container Problem still applies; static multi-dispatch (method overloading) isn’t enough, nor are parametric types: Person lineUp[] = { new Student(" Chris "), new Person(" Paul "), new Person(" Duane "), new Student(" Steve "), new Person(" Wade ")}; for (int i=0; i


Download ppt "Multi-Dispatch in the Java Virtual Machine TM What is (dynamic) multi-dispatch? Method selection based on the types of … all (or more than one) of the."

Similar presentations


Ads by Google