Concepts of Programming Languages

(Sean Pound) #1
12.4 Support for Object-Oriented Programming in Smalltalk 535

hides that of the ancestor class. Access to such a hidden method is provided by
prefixing the message with the pseudovariable super. The prefix causes the
method search to begin in the superclass rather than locally.
Because entities in a parent class cannot be hidden from subclasses, all
subclasses are subtypes.
Smalltalk supports single inheritance; it does not allow multiple inheritance.

12.4.3 Dynamic Binding


The dynamic binding of messages to methods in Smalltalk operates as follows:
A message to an object causes a search of the class to which the object belongs
for a corresponding method. If the search fails, it is continued in the super-
class of that class, and so forth, up to the system class, Object, which has no
superclass. Object is the root of the class derivation tree on which every class
is a node. If no method is found anywhere in that chain, an error occurs. It
is important to remember that this method search is dynamic—it takes place
when the message is sent. Smalltalk does not, under any circumstances, bind
messages to methods statically.
The only type checking in Smalltalk is dynamic, and the only type error
occurs when a message is sent to an object that has no matching method, either
locally or through inheritance. This is a different concept of type checking than
that of most other languages. Smalltalk type checking has the simple goal of
ensuring that a message matches some method.
Smalltalk variables are not typed; any name can be bound to any object. As
a direct result, Smalltalk supports dynamic polymorphism. All Smalltalk code is
generic in the sense that the types of the variables are irrelevant, as long as they
are consistent. The meaning of an operation (method or operator) on a variable
is determined by the class of the object to which the variable is currently bound.
The point of this discussion is that as long as the objects referenced in an
expression have methods for the messages of the expression, the types of the
objects are irrelevant. This means that no code is tied to a particular type.

12.4.4 Evaluation of Smalltalk


Smalltalk is a small language, although the Smalltalk system is large. The syn-
tax of the language is simple and highly regular. It is a good example of the
power that can be provided by a small language if that language is built around
a simple but powerful concept. In the case of Smalltalk, that concept is that all
programming can be done employing only a class hierarchy built using inheri-
tance, objects, and message passing.
In comparison with conventional compiled imperative-language programs,
equivalent Smalltalk programs are significantly slower. Although it is theo-
retically interesting that array indexing and loops can be provided within the
message-passing model, efficiency is an important factor in the evaluation of
programming languages. Therefore, efficiency will clearly be an issue in most
discussions of the practical applicability of Smalltalk.
Free download pdf