The class concept
- abstract data type -- interface description
- object generator -- template for creation
- repository -- for sharing resources
- object -- instance of a metaclass
slide: The concept of class
In addition, however, in a number of systems
a class may be used as a repository for sharing
class-wide resources.
For example, the Smalltalk language allows the definition of
class variables that are accessible to all instances of the class.
See slide [5-class].
Class variables
Clearly, the use of class variables
violates what we have called the distribution boundary
in section [multi-paradigm],
since it allows objects to reach out of their
encapsulation borders.
Class variables may also be employed in C++ and Java
by defining data members as static.
Apart from class variables, Smalltalk also supports
the notion of class methods,
which may be regarded as routines having the class
and its instances as their scope.
Class methods in Smalltalk are typically used
for the creation and initialization of new instances of the class
for which they are defined.
In C++ and Java, creation and initialization is taken care
of by the constructor(s) of a class,
together with the (system supplied) new
operator.
Class methods, in C++ and Java, take the form
of static member functions that are like
ordinary functions
(apart from their restricted scope and their calling syntax,
which is of the form in C++ and in Java).
Contrary to classes in C++ and Java,
classes in Smalltalk have a functionality
similar to that of objects.
Classes in Smalltalk provide
encapsulation (encompassing class variables and
class methods) and message passing
(for example for the creation and initialization
of new instances).
To account for this object-like behavior,
the designers of Smalltalk have introduced the notion
of metaclass of which a class is an instance.
Metaclasses
In the classical object model, two relations
play a role when describing the
architectural properties of a system.
The first relation is the instance relation
to indicate that an object O is an instance of a class
C.
The second (equally important) relation is
the inheritance relation,
which indicates that a class C is a subclass
(or derived from) a given (ancestor) class P.
When adopting the philosophy
everything is an object
together with the idea that
each object is an instance of a class
(as the developers of Smalltalk did),
we evidently get into problems when we try to explain
the nature (and existence) of a class.
To be an object, a class itself must be an instance
of a class (which for convenience we will call
a metaclass).
Take, for example, the class Point.
This class must be an instance of a (meta)class
(say Class) which in its turn must be an instance of a (meta)
class (say MetaClass),
and so on.
Clearly, following the instance relation
leads to an infinite regress.
Hence, we must postulate some
system-defined MetaClass (at a certain level)
from which to instantiate the (metaclasses of)
actual classes such as Point.