Smalltalk superclass vs metaclass?
Asked Answered
O

4

9

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...

Obstinate answered 25/1, 2010 at 4:14 Comment(0)
M
9

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

Mcdaniels answered 25/1, 2010 at 7:26 Comment(3)
Thanks for the link! Seems to be what I'm looking for.Obstinate
You should include the gist of the book that answers this question instead of just referring to a link. Once that link get broken, this answer becomes no longer usefulInflexed
The fears of the previous commenter have been confirmed. The link is broken.Guidotti
M
7

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.

Mcquade answered 25/1, 2010 at 4:23 Comment(3)
@ zneak -> "The metaclass is "simply" the class of the class object." I thought that the superclass was "the class of the class object". That's my question really - what's the diff between superclass and metaclass?Obstinate
"There are actually two levels of inheritance: instance inheritance and class inheritance." From this formulation, I'd guess you're confusing inheritance and instanciation…Madalene
This is really what I meant. Subclasses can override what we'd call static methods in other languages.Mcquade
M
4

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.

Madalene answered 2/2, 2010 at 12:51 Comment(0)
D
0

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.

Designed answered 13/11, 2015 at 1:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.