Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Metaclass
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==In Smalltalk-80== {{unreferenced section|date=October 2013}} [[File:Smalltalk 80 metaclasses.svg|thumb|The Smalltalk-80 metaclass hierarchy as a UML diagram]] [[File:Smalltalk metaclass.png|thumb|Diagram of the inheritance and instance relationships between classes and metaclasses in Smalltalk]] In [[Smalltalk]], everything is an [[Object (computer science)|object]]. Additionally, Smalltalk is a [[Class-based programming|class based]] system, which means that every object has a class that defines the structure of that object (i.e. the instance variables the object has) and the messages an object understands. Together this implies that a class in Smalltalk is an object and that, therefore a class needs to be an instance of a class (called metaclass). As an example, a car object <code>c</code> is an instance of the class <code>Car</code>. In turn, the class <code>Car</code> is again an object and as such an instance of the metaclass of <code>Car</code> called <code>Car class</code>. Note the blank in the name of the metaclass. The name of the metaclass is the Smalltalk expression that, when evaluated, results in the metaclass object. Thus evaluating <code>Car class</code> results in the metaclass object for <code>Car</code> whose name is <code>Car class</code> (one can confirm this by evaluating <code>Car class name</code> which returns the name of the metaclass of <code>Car</code>.) [[Class method]]s actually belong to the metaclass, just as instance methods actually belong to the class. When a message is sent to the object <code>2</code>, the search for the method starts in <code>Integer</code>. If it is not found it proceeds up the superclass chain, stopping at Object whether it is found or not. When a message is sent to <code>Integer</code> the search for the method starts in <code>Integer class</code> and proceeds up the superclass chain to <code>Object class</code>. Note that, so far, the metaclass inheritance chain exactly follows that of the class inheritance chain. But the metaclass chain extends further because <code>Object class</code> is the subclass of <code>Class</code>. All metaclasses are subclasses of Class. In early Smalltalks, there was only one metaclass called <code>Class</code>. This implied that the [[method (computer science)|methods]] all classes have were the same, in particular the method to create new objects, i.e., <code>new</code>. To allow classes to have their own methods and their own instance variables (called class instance variables and should not be confused with [[class variable]]s), Smalltalk-80 introduced for each class <code>C</code> their own metaclass <code>C class</code>. This means that each metaclass is effectively a [[Singleton pattern|singleton]] class. Since there is no requirement that metaclasses behave differently from each other, all metaclasses are instances of only one class called <code>Metaclass</code>. The metaclass of <code>Metaclass</code> is called <code>Metaclass class</code> which again is an instance of class <code>Metaclass</code>. In Smalltalk-80, every class (except <code>Object</code>) has a [[superclass (computer science)|superclass]]. The [[abstract superclass]] of all metaclasses is <code>Class</code>, which describes the general nature of classes. The superclass hierarchy for metaclasses parallels that for classes, except for class <code>Object</code>. ALL metaclasses are subclasses of <code>Class</code>, therefore: * <code>Object class superclass == Class.</code> Like [[conjoined twins]], classes and metaclasses are born together. <code>Metaclass</code> has an instance variable <code>thisClass</code>, which points to its conjoined class. Note that the usual Smalltalk [[class browser]] does not show metaclasses as separate classes. Instead the class browser allows to edit the class together with its metaclass at the same time. The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance: * <code>Object</code> is the base class that provides common methods for all objects; "an object" is an integer, or a widget, or a <code>Car</code>, etc. * <code>Class</code> is the base of the metaclasses that provides common methods for all classes (though it is not a metaclass itself); "a class" is something like <code>Integer</code>, or <code>Widget</code>, or <code>Car</code>, etc. * <code>Metaclass</code> provides common methods for all metaclasses. Four classes provide the facilities to describe new classes. Their [[inheritance hierarchy]] (from Object), and the main facilities they provide are: :Object - default behavior common to all objects, like class access ::Behavior - minimum [[State (computer science)|state]] for [[compiling]] methods and creating/running objects :::ClassDescription ([[abstract class]]) - class/variable naming, comments ::::Class - similar, more comprehensive, facilities to superclasses ::::Metaclass - initializing class variables, instance creation messages
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)