Static and Dynamic Method Binding

Now we turn to another question, that is the issue of static and dynamic method binding. The issue here is how to bind a message to a method body. Should this be based on the declared type or the dynamic type?

Here is a story to give you an intuition concerning this issue. Alice is holding a small mammal, and talking to Bill. Bill cannot see what type of animal Alice is holding, but knows that it is a mammal. Alice asks Bill ``Does this animal give birth to live young?''

The static answer is yes, since Bill knows that all Mammals give birth to live young.

But what if the animal is Phyl, our pet platypus from the cover of the book? Platypus lay eggs. And therefore the dynamic answer would be no.

Both varieties of bindings are found in programming languages. Even statically typed object-oriented languages will sometimes use dynamic binding. But even when they do, they often use the static type to determine the legality of an operation. That is, they will issue an error message if the static type cannot be used to answer the message.

[audio] [real] Text to accompany slide12, in Chapter 10 of An Introduction to Object-Oriented Programming