Not all object-oriented programming languages support metaclasses. Among those which do, the extent to which metaclasses can override any given aspect of class behavior varies. Each language has its own metaobject protocol, a set of rules which govern how objects, classes, and metaclasses interact.
In Python, the builtin class
type is a metaclass. Consider this simple Python class:
At run time,
Car itself is a
type object. The source code of the
Car class, shown above, does not include such details as the size in bytes of
Car objects, their binary layout in memory, how they are allocated, that the
__init__ method is automatically called each time a
Car is created, and so on. These details come into play not only when a new
Car object is created, but also each time any attribute of a
Car is accessed. In languages without metaclasses, these details are defined by the language specification and can't be overridden. In Python, the metaclass,
type, controls these details of
Car's behavior. They can be overridden by using a different metaclass instead of
The above example contains some redundant code to do with the four attributes
color. It is possible to eliminate some of this redundancy using a metaclass. In Python, a metaclass is most easily defined as a subclass of
This metaclass only overrides object creation. All other aspects of class and object behavior are still handled by
Now the class
Car can be rewritten to use this metaclass. This is done by assigning to
__metaclass__ within the class definition:
Car objects can then be instantiated like this:
Metaclass programming can be confusing, and it is rare in real-world Python code.
In Smalltalk, everything is an object. There are two kinds of objects: those which can create instances of themselves (classes), and others which cannot. Every object is the instance of a class. Every class is the instance of a metaclass.
In early Smalltalks, there was one metaclass called
Class. The object creation method of all classes was the same, i.e.,
new. A class sent the message
new could only return an object with uninitialized instance variables. Smalltalk's designers wanted to send one message to an object to initiate both creation and initializaton. They achieved this in Smalltalk-80.
In Smalltalk-80, a class is an instance of its own metaclass; and each class can have unique methods for creating objects. Metaclasses, like other classes, contain methods used by their instances. But metaclasses are all instances of one class, called
Metaclass. Unlike classes, metaclasses do not need flexibile creation methods, because classes all have the same structure. For instance, the class
Car has instance variables just like any other class. People using (and not re-designing) Smalltalk do not need to write class creation methods.
Names are not given to metaclasses. The metaclass of class
Sphere is simply referred to as "the metaclass of class
Sphere". The metaclass of a class may be accessed by sending the message
class to the class.
The methods of a metaclass create instances, and initialize class variables.
The superclass hierarchy for metaclasses parallels that for classes, except for class
Object. ALL metaclasses are subclasses of
Object class superclass == Class.
Like conjoined twins, classes and metaclasses are born together.
Metaclass has an instance variable
thisClass, which points to its conjoined class.
The names of classes in the metaclass hierarchy are easily confused with the concepts of the same name. For instance:
Objectis the base class which provides common methods for all objects; "an object" is an integer, or a widget, or a
Classis the base metaclass which provides common methods for all classes; "a class" is something like
Metaclasshas the same relation to "a Metaclass".
Four classes provide the facilities to describe new classes. Their inheritance hierarchy (from Object), and the main facilities they provide are:
Class methods actually belong to the metaclass, just as instance methods actually belong to the class. When a message is sent to the object 2 the search for the method starts in
Integer. If it not found it proceeds up the superclass chain, stopping at Object whether it is found or not.
Aside - another way of saying "metaclass of
When a message is sent to
Integer the search for the method starts in
Integer class and proceeds up the superclass chain to
Object class. Note that, so far, the metaclass inheritance chain exactly follows that of the class inheritance chain. But the metaclass chain extends further because
Object class is the subclass of
Class. All metaclasses are subclasses of Class.
All metaclasses are instances of class
Metaclass. So the metaclass of
Metaclass is an instance of Metaclass.
The following programming languages support metaclasses.
Some less widespread languages which support metaclasses include OpenJava, OpenC++, OpenAda, CorbaScript, ObjVLisp, Object-Z, MODEL-K, XOTcl, and MELDC. Several of these languages date from the early 1990s and are of academic interest.