The Smalltalk object model is based on a limited number of concepts applied uniformly. Smalltalk’s designers applied Occam’s razor: any consideration leading to a model more complex than necessary was discarded.
To refresh your memory, here are the rules of the object model that we explored in Chapter 5.
Rule 1. Everything is an object.
Rule 2. Every object is an instance of a class.
Rule 3. Every class has a superclass.
Rule 4. Everything happens by message sends.
Rule 5. Method lookup follows the inheritance chain.
As we mentioned in the introduction to this chapter, a consequence of Rule 1 is that classes are objects too, so Rule 2 tells us that classes must also be instances of classes. The class of a class is called a metaclass. A metaclass is created automatically for you whenever you create a class. Most of the time you do not need to care or think about metaclasses. However, every time that you use the system browser to browse the “class side” of a class, it is helpful to recall that you are actually browsing a different class. A class and its metaclass are two separate classes, even though the former is an instance of the latter.
To properly explain classes and metaclasses, we need to extend the rules from Chapter 5 with the following additional rules.
Rule 6. Every class is an instance of a metaclass.
Rule 7. The metaclass hierarchy parallels the class hierarchy.
Rule 8. Every metaclass inherits from
Rule 9. Every metaclass is an instance of
Rule 10. The metaclass of
Metaclass is an instance of
Together, these 10 rules complete Smalltalk’s object model.
We will first briefly revisit the 5 rules from Chapter 5 with a small example. Then we will take a closer look at the new rules, using the same example.