ch10 3
ch10 3
Fall 2003
Dynamic Binding
(Section 10.4)
1
Fundamental 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
2
Inheritance
3
Inheritance
Notation
Base Class
Java.awt.Dialog (or Parent Class
or Superclass)
Is-a relationship
Derived Class
Java.awt.FileDialog (or Child Class
or Subclass)
4
Dynamic 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
• Example (C++):
class person { ... };
class student : public person { ... };
class professor : public person { ... };
student s;
professor p;
...
person * x = &s;
// Both student and professor objects have all properties of
person * y = &p;
// a person object
// Both can be used in a person context
5
Dynamic Method Binding
• Example (C++):
class person { ... };
class student : public person { ... };
class professor : public person { ... };
student s;
professor p;
...
person * x = &s;
person * y = &p;
6
Dynamic Method Binding
• Example (C++):
– Suppose that we redefine print_mailing_label in both derived classes
7
Dynamic Method Binding
• Example (C++): student s;
professor p;
...
person * x = &s;
person * y = &p;
x->print_mailing_label (); // ??
– Two alternatives for choosing the
y->print_mailing_label (); // ??
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)
• Java, Eiffel
– dynamic method binding by default
– individual methods can be labeled final (Java) or frozen (Eiffel)
» cannot be overriden by derived classes
» use static method binding
• Terminology in C++:
– redefine a method that uses static binding
– override a method that uses dynamic binding
• C++
class person
{
public:
virtual void print_mailing_label ();
...
}
10
Virtual and Non-Virtual Methods
• Ada
14
Member Lookup
• Implementation of a vtable:
– Ordering is essential:
» all new members introduced by bar must appear at the end:
» additional data members (w) at the end of the record
» additional virtual methods (s and t) at the end of the vtable
» virtual methods overridden in bar (such as m) – appear in bar's vtable at the
same place as in foo's vtable
16
Member Lookup
17
Type Checking
• C++ foo F;
bar B;
class foo { ... }; foo * q;
class bar : public foo { ... }; bar * s;
...
q = &B;
s = &F;
18
Type Checking
• Java
– allows only the "classic" cast, but with dynamic check
• Eiffel
class foo ...
class bar inherit foo ...
...
f : foo;
b : bar;
...
f := b; -- always ok
b ?= f; -- reverse assignment, with dynamic check
• Smalltalk
– variables are untyped references
– data members are never public
– any assignment is legal
– only method invocation ("send a message") is dynamically checked
» implementation – dictionary that maps method names to their code
» run-time – lookup in dictionary to check if method is supported
» if not – dynamic semantic error
21
Classes as "Closures"
22
Classes as "Closures"
• Application:
– discrete event simulation
– need to have a subroutine schedule_at that:
» takes as parameters a time value and a function f (to be called at that time)
» function f should have an arbitrary set of parameters
– how do we pass such a function f (with ANY number of parameters)?
24