Title: Dynamic Binding (Section 10.4)
1Dynamic Binding(Section 10.4)
- CSCI 431 Programming Languages
- Fall 2003
A compilation of material developed by Felix
Hernandez-Campos and Mircea Nicolescu
2Fundamental Concepts in OOP
- Encapsulation
- Data Abstraction
- Information hiding
- The notion of class and object
- Inheritance
- Code reusability
- Is-a vs. has-a relationships
- Polymorphism
- Dynamic method binding
3Inheritance
- Encapsulation improves code reusability
- Abstract Data Types
- Modules
- Classes
- However, it is generally the case that the code a
programmer wants to reuse is close but not
exactly what the programmer needs - Inheritance provides a mechanism to extend or
refine units of encapsulation - By adding or overriding methods
- By adding attributes
4InheritanceNotation
Base Class (or Parent Class or Superclass)
Java.awt.Dialog
Is-a relationship
Derived Class (or Child Class or Subclass)
Java.awt.FileDialog
5Dynamic Method Binding
- Consequence of inheritance
- derived class D has all members of its base class
B
- can use an object of class D everywhere an object
of class B is expected - a form of polymorphism
// Both student and professor objects have all
properties of // a person object // Both can be
used in a person context
6Dynamic Method Binding
7Dynamic Method Binding
8Dynamic Method Binding
- Two alternatives for choosing the method to call
- according to the types of variables (references)
x and y static method binding (will call the
method of person in both cases) - according to the types of objects s and p to
which x and y refer dynamic method binding
(will call the methods of student / professor)
9Dynamic Method Binding
- Simula, C, Ada 95
- static method binding by default
- how do we specify dynamic binding in C?
- label individual methods as virtual
10Virtual and Non-Virtual Methods
11Virtual and Non-Virtual Methods
12Virtual and Non-Virtual Methods
13Abstract Classes
14Member Lookup
15Member Lookup
- The compiler will generate
16Member Lookup
17Member Lookup
ok references through q will use prefixes of B's
data space and vtable
static semantic error F lacks the additional
data and vtable entries of a bar
18Type Checking
still an error
ok, but risky programmer must ensure that q
refers to a bar object
C specific involves dynamic type check
19Type Checking
20Type Checking Implementation
21Member Lookup
- Disadvantages of virtual methods
- run-time cost
- preclude in-line expansion of methods at compile
time - performance decrease for small and frequently
called methods
- Potential problem with static lookup
- fragile base class problem (Java)
- large standard library, evolving over time
(adding new features) - user may have old library and run code designed
for new library - code may use the new features
- static lookup invalid memory access
- dynamic lookup better, produce error message
("member not found")
22Classes as "Closures"
23Classes as "Closures"
24Classes as "Closures"