Question

I new to OOP, but with a "procedural" background.

I'm currently trying to get my head around OOP via GNU Smalltalk and Lovejoy's "Smalltalk: Getting The Message".

I'm confused as to the the heck the metaclass and Metaclass class are, vs superclass. I can see the inheritance flow of superclass -> class -> subclass; but I don't see how/where metaclass fits in. TIA...

Was it helpful?

Solution

There is an excellent description in the free online book Pharo by Example, Chapter 13 (Classes and metaclasses). The things explained in this chapter are common to all Smalltalk implementations.

OTHER TIPS

There are actually two levels of inheritance: instance inheritance and class inheritance.

Smalltalk has a special scheme that makes it possible to pass around classes as objects. That means classes are also objects in their own rights. The metaclass is "simply" the class of the class object.

It doesn't interfere with normal instance inheritance, so it doesn't fit anywhere in the superclass -> class -> subclass diagram you used.

There are two different relations in class-based OO: instantiation and inheritance.

Instantiation is the relation between an object and its class, the new keyword, etc. Usually it's implemented by a pointer in the low-level representation of any object. In Smalltalk, anObject class traverses this pointer; it also happens that classes are also objects, and classes of classes are called metaclasses, but this is the same relation as with instances.

Inheritance is a relationship between classes. You can go from a class to its superclass by doing aClass superclass, and keep doing so until you get to the class Object. In Smalltalk, the superclass pointer is just an instance variable defined on all classes, and the superclass message is a normal accessor.

As a concept, a superclass is the parent of an object's Class. i.e. the Class one level higher in the Class hierarchy than the Class of the current object.

As a named method, it returns the name of the immediate superclass of the receiver. e.g. it's definedin Squeak Smalltalk (and also in its derivatives, Pharo and Cuis) as superclass "Answer the receiver's superclass, a Class." ^superclass

In Dolphin Smalltalk, it's defined as `superclass "Answer a which is the receiver's immediate superclass (or if none)."

^superclass'

But - every Class in the Class hierarchy is actually an instance of its parent class. So the Class that a given Class is an instance of, is the Class's MetaClass.

So, e.g. aSortedCollection is an object - an instance of the Class SortedCollection.

SortedCollection is a Class - named Class 'SortedCollection' in the browsable Class hierarchy. Simultaneously, it is also an instance of a Metaclass - an anonymous Class which has a singleton object instance, which is a named Class. The named class is visible in the Class hierarchy, but the Metaclass (being anonymous) is much less visible. It is there so that Smalltalk

Smalltalk maintains a Metaclass hierarchy, i.e. a hierarchy of the Classes of the Classes. It's much less visible, as it's held as anonymous system objects, but you can find the top level of the Metaclass hierarchy in the Class browser. Both the Class Class and the Class Metaclass are to be found as sub-sub-classes of Class Behaviour, itself a sub-class of Class Object.

One reason that people say that "In Smalltalk, everything is an object" is because Class Object is the root of all the other Classes and objects - it is at the very top of the object hierarchy, which contains the Class hierarchy, and the Metaclass hierarchy.

(It's generally at this stage that my brain begins to bleed out of my ears, but th following 3 points help push it all back in to my skull)

If you send the message anInstanceOfAClass class - you'll get
the Class of the object anInstanceOfAClass returned.

If you send the message anInstanceOfAClass class superclass - you'll get
the parent Class of the Class of the object anInstanceOfAClass returned.

If you send the message anInstanceOfAClass class class - you'll get the anonymous singleton Metaclass of the Class of the object anInstanceOfAClass returned.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top