Professional Documents
Culture Documents
Dynamic Binding in Java
Dynamic Binding in Java
Dynamic Binding in Java
Dynamic Binding
Binding Binding time Motivation Dynamic Binding High Level Methods
oop
Binding: linking between messages and methods Inclusion Polymorphism + Overriding = Binding Question
Binding Time
The same entity may refer to objects of different classes, each of which has a different implementation of the same method
More generally, binding time is the time when an attribute of some portion of a program, or the meaning of a particular construct is determined Compile Time Link Time Execution Time:
Program Init Procedure/function begin Statement Execution
Static Binding (AKA Early Binding): the compiler uses the type of variables to do the binding at a compile (link) time Dynamic Binding (AKA Late Binding): the decision is made at run time based on the type of the actual values
oop
AddRealNums(x, y) x, y - of type real. Late (dynamic) binding of the message selector to the appropriate method at run time.
The programmer doesn't know the specific method that will be used. For example:
Manager M; M.raise_salary(10);
What is the type of this in the called method? Static Type: Employee *
What can be determined in compile time Dynamic Type:
Manager *
Manager print++
Actual type, as determined in run time No simple way for the programmer to examine the dynamic type
Suppose that raise_salary calls print, then, which version of print will be called?
oop
oop
Motivation I: Containers
Employee print+
Manager print++
Engineer print++
SalesPerson print++
#define SIZEOF(a) (sizeof(a)/sizeof(*a)) ... Manager Alice, Bob; Engineer Chuck, Dan, Eve; SalesPerson Faith, Gil, Hugh; Which print should Employee Ira, Jim, Kelly; .... be called here? Employee *delegation[] = { &Bob, &Dan, &Faith, &Kelly}; ... for (int i = 0; i < SIZEOF(delegation); i++) delegation[i]->print(cout);
oop
We want the list to contain elements of type Shape, but each should be capable of preserving its own special properties. If we loop over the list and send each element in it a draw message, the element should respond according to its dynamic type.
oop
High Level Methods: methods which are coded using only the public interface of a class. Shape Facilitate code reuse in inheritance. location
class Shape { Point location; public: Which hide should be ... void move(Point delta) called here? { hide(); location += delta; Which draw should be called here? draw(); } ... }; class Rectangle: public Shape { public: void hide(void) const { ... } void draw(void) const { ... } };
oop
void duplicate(const Shape *base) { enum {Dx = 12, Dy = 30}; Shape *clone = base->clone(); clone->move(Dx,Dy); clone->draw(); }
clone+, draw+
Star
clone++, draw++
10
The rule: A called method should be selected based on the actual type of the object it is sent to.
All methods in Smalltalk, Eiffel and all other decent object-oriented programming languages are dynamically bound.
oop
Dynamic Binding in C?
enum Shape_type { rectangle, circle, line, ... };
11
struct Shape { int x, y; ... /* Other common fields */ Shape_type type; union { struct Rectangle { ... } r; struct Circle { ... } c; struct Line { ... } l; ... /* Other shape kinds */ } data; };
oop
Strong Coupling
void rotate(Shape *p) { switch (p->type) { case rectangle: ... case circle: ... case line: ... ... } pointers to }
void draw(Shape *p) { switch (p->type) { case rectangle: ... case circle: ... case line: ... ... } }
12
Dispersed coding: the code for different operations on the same shape is spread all over the program.
Difficult to understand
Insusceptibility to change: whenever a new kind of shape is introduced, the following must be changed:
Definition of Shape_type Definition of Shape Some OO programming Function rotate languages do not include a Function draw switch like statement, just All other functions because of the above bad usage
of switch!
oop
13
The specialized code is associated with the type, not with each function. OOP promotes the shift of focus from operations to operands
class Shape { public: virtual void draw(void); virtual void hide(void); virtual void rotate(int deg); ... protected: int x, y; ... /* Other common fields */ };
class Circle: public Shape { public: virtual void draw(void) { ... } virtual void hide(void) { ... } //... The type tag is hidden, and }; class Line: public Shape { maintained by the compiler public: virtual void draw(void) { ... } virtual void hide(void) { ... } //... }; class Rectangle: public Shape { public: virtual void draw(void) { ... } virtual void hide(void) { ... } //... };
oop
14
15
Usage of RTTI in all but very special cases indicates a misunderstanding of the power of dynamic binding.
oop
16
Low level methods: implemented only using data members code not affected by overriding High level methods: may use also other methods partially polymorphic code Outer level methods: methods implemented solely with (externally accessible) methods fully polymorphic code
oop
17
Static typing + Dynamic binding: the right combination of safety and power
Examples: Eiffel, C++, Java, Object-Pascal, Turbo-Pascal, etc.
oop
18
C++ Example
struct Base { virtual void f(void) { ... } void g(void) { ...} }; struct Derived: public Base { virtual void f(void) { ...} // Override f() of Base void g(void) { ...} // Override g() of Base void h(void) { ...} } y; Base *px = &y; px->f(); // The right method px->g(); // The wrong method px->h(); // Compile time error! No guarantee that the method exists
oop
19
Static method binding may give better error detection, just as static typing gives better error detection. Usage:
Static binding: homogenous set of objects that are similar to an existing class; inheritance simply makes the new set of objects easier to implement. Dynamic binding: heterogeneous mix of objects that share operations that are conceptually the same, but may have different implementations.
oop