Inheritance and Dynamic Binding Jia Chapter 5 Static
Inheritance and Dynamic Binding Jia Chapter 5
Static and Dynamic Type l Static Type – The type of an object specified in its declaration. Dynamic Type-- The type of an object as a program executes. Mammal m = new Human(); m has a static type of Mammal, and a dynamic type of Human.
Class Inheritance Hierarchy abstract class Mammal { // some abstract methods // some concrete methods} class Human extends Mammal { // implementations of Mammal’s abstract // overrides of Mammal’s concrete // additional methods } class Whale extends Mammal {…}
Polymorphism l Polymorphic object-- An object whose dynamic type changes. In most O. O. languages, an object cannot change into just anything, only to a subtype of its static type. Mammal m; Whale w = new Whale(); m = w; m = new Human(); l What do we know about the Human and Whale?
Subsitutability of Subtypes l (Jia 167): A value of a subtype can always substitute for a value of its supertype. l l Dynamic type is always subtype (descendent) of static type (or it is the static type). Restricted polymorphism Mammal m = new Human() is widening Human h = mammal. List[i] is narrowing l Requires casting… l Human h = (Human) mammal. List[i];
Dynamic (method) Binding l When a method invocation is bound to a specific implementation of the method at run-time not compile-time. What is dynamically bound in the following? What is polymorphic? Mammal m; for (i=0; i<mammal. Arr. length; i++) { m = mammal. Arr[i]; m. eat(); }
Static vs. Dynamic Binding Static Binding Source Code Generated Assembly m. eat() jmp 8094562 Dynamic Binding m. eat() check m’s dynamic type use it to jmp xxxxxxx
In-Class Assignment l Modify the University code at http: //cs. usfca. edu/~wolber/Software. Dev/OO/ Student. Example/No. Inheritance/ so that inheritance and dynamic binding is used to process the student information.
Under the Hood: VTable l l Dynamic binding is quite efficient Especially if dynamically bound call replaces if/switch statement
Elegant Processing of Polymorphic Lists for (int i=0; i<list. size(); i++) { object = (Mammal) list. get(i); object. do. Something. Generic(); object. do. Something. Special(); } // without inheritance/dynamic binding use switch for (int i=0; i<list. size(); i++) { object = (Mammal) list. get(i); object. do. Something. Generic(); switch (object. type) { case HUMAN: object. swim. Like. AHuman(); case WHALE: object. swim. Like. AWhale(); } }
Elegant Handling of Polymorphic Parameters class Some. Client. Class { some. Method(Mammal m) { m. general(); m. special(); // no switches or if-else necessary }
Design Expecting Change! l What if a new subtype of Mammal needs to be added to the system? How is dynamic binding solution better?
Dynamic Binding Benefit l Program modifications are localized l l l Just add a subtype Need not modify possibly hundreds of“clients” that call services of superclass (e. g. , no adding new cases to numerous switch statements). Note that this allows: l UNCHANGEABLE LIBRARY CODE TO CHANGE MAGICALLY!
Frameworks Library Code super. Class 1. m() super. Class 2. n(); … m and n are called hook methods Clients/Library Extenders can: 1. Write subclasses that extend behavior of library classes. 2. 2. Cause methods in library to “magically” call newly added functionality
Template Method Pattern l Template method provides skeleton of algorithm, leaving some details to subclasses… class Some. Framework. Class { template. Method() { // maybe looping through list… superclass. generic(); superclass. hook(); … l Template method calls one or more “hook” methods.
Programming to An Interface: List Example l Specify the concrete type only on new List l = new Linked. List(); l In all methods that use l, refer only to “List” and not “Linked. List” or “Array. List”: meth 1(List l) {…} l … {…} methn(List l) Later, if it is decided that Array. List impl. preferable, change in one place only: List l = new Array. List();
- Slides: 16