Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level.

Similar presentations


Presentation on theme: "1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level."— Presentation transcript:

1 1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level System  1 Level System  Infinite Levels System

2 2 Class Representation in Memory? A class is defined in the source code, so why should it be represented in the runtime environment? Answer: Dynamic Binding! The actual method bound to a message is determined at run time based on the type (class) of an object. There must be a link between the object and its methods. Since these links are the same for all objects of the same class, it makes sense to share the representation. Schematic diagram, common to all OO languages: Class Rep Method 1 Method 2 Object Method n Msg Methods Table

3 3 A Class as an Object? Dynamic binding implies that classes must have some memory representation. Why not as an object? Answer: Let's go for it! Each object o has a pointer to its class object c which is the object representing the class to which o belongs.

4 4Metaclasses Meta-: Beyond; transcending; more comprehensive; e.g., as in metalinguistics. Class: generates instances. Metaclass: a class whose instances are classes. Terminal Instance: cannot be instantiated further. Main Text Book Reference: G.Masini et al. Object-Oriented Languages, APIC series, No.34

5 5 Taxonomy of Metaclass Systems 1-Level System: Objects only Each object describes itself No need for classes: objects are “instantiated” or “inherited” from other objects Example: Self, Javascript 2-Level System: Objects, Classes Objects: described by classes Classes: need no description--do not exist in run-time. Not first- class objects: Not instances of classes Not created by constructors Cannot receive messages Examples: Eiffel 3-Level System: Objects, Classes, Metaclass Objects: described by classes Classes: described by the metaclass The Metaclass: describes itself Examples: Smalltalk-76, Little-Smalltalk

6 6 oop The 3 Level System

7 7 One Metaclass Structure Simple and elegant model: courtesy of Smalltalk-76, the first language to introduce the metaclass concept. Later adopted by Little-Smalltalk The Class metaclass: The only metaclass in the system Instance of itself Avoids the infinite regress of the instantiation relationship Inherits from Object Holds behaviour common to all class objects: How to add methods How to instantiate a class Holds structure common to all class objects: Super class Instance variables List of methods Uniform behaviour and structure of all classes: Different classes cannot have different new methods The new defined in class Class calls new defined in the object’s class

8 8 oop The 1 Level System

9 9 Principles of the 1 Level Sysem In a 1-level system, each object describes itself. An object is a essentially a map from names (strings) to either values or methods Very flexible, but static type checking is hard No distinction between objects and classes Every object is the class of itself Instantiation of a new object: Take an existing object and clone it.

10 10 Class Variables and Class Methods If a class is an object, then it makes sense to ask what are the variables and methods of that object. Simple answer (LST approach): Class Methods: Constructor! Send a message to the class, requesting it to create a new instance. Class Variables: Data structure defining the structure of instances. Data structure determining the protocol/implementation of instances. No per-class variables The more sophisticated answer: Smalltalk-80 (a 5 level system): Class specific variables Class specific methods

11 11 oop A 4 Level System

12 12 Properties of the 4 Level System Each object: is an instance of a class Each class: is an instance of a metaclass May have its own singleton metaclass May share a metaclass with other classes May use the default metaclass Meta Object Each metaclass: is an instance of a meta-metaclass. One meta-metaclass: Instance of itself Inherits from Object (or Class)‏ Examples: (with minor variations)‏ LOOPS: Lisp Object and data Oriented Programming System ObjVlisp: OBJect Virtual extension of LISP A model more than a real language

13 13 oop Infinite Object-Class Regression  3-KRS: an infinite number of levels system Objects: instances of “metaobjects” (aka classes). Classes: instances of metaclasses. Metaclasses: instances of meta-metaclasses. Meta-metaclasses: instances of meta-meta- metaclasses.... For each object X, there is a meta-object  X Lazy creation of meta-objects enables the system to work in a finite world.

14 14 oop A 5 Level System Three Kinds of Entities: Objects Classes: each class X has an object X describing it. Integer Object Class: the class of all class objects Metaclass: the class of all metaclass objects Metaclasses: the object X describing class X is an instance of a singleton metaclass: “X class” describing it. Integer class: the class of “Integer” Class class: the class of “Class” Metaclass Class: the class of “Metaclass” The metaclass “Novel class” is also the result of evaluating the Smalltalk expression: Novel class The metaclass “Novel class” is also the result of evaluating the Smalltalk expression: Novel class

15 15 oop Inheritance in the 5 Level System A metaclass object “X class”: is an instance of “Metaclass” inherits from “Class” Class: an abstract class parent of all “X class” objects Metaclasses hierarchy: X inherits from Y “X class” inherits from “Y class”


Download ppt "1 Case Study: Meta Classes  Class representation in memory  Class variables and class methods  Meta Classes  3 Level System  4 Level System  5 Level."

Similar presentations


Ads by Google