SlideShare a Scribd company logo
1 of 28
S.Ducasse 1
Metaclasses in 7 Steps
Classes are objects too...
Classes are instances of other classes
...
One model applied twice
S.Ducasse 2
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
Adapted from Goldberg & Robson, Smalltalk-80 — The Language
S.Ducasse 3
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 4
1. Every object is an instance of a class
S.Ducasse 5
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from
Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 6
2. Every class inherits from Object
Every object is-an Object
The class of every object
ultimately inherits from Object
S.Ducasse 7
The Meaning of is-a
When an object receives a message, the method is
looked up in the method dictionary of its class, and,
if necessary, its superclasses, up to Object
S.Ducasse 8
Responsibilities of Object
Object
represents the common object behavior
error-handling, halting …
all classes should inherit ultimately from Object
S.Ducasse 9
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a
metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 10
3. Every class is an instance of a metaclass
Classes are objects too!
Every class X is the unique instance of its metaclass, called
X class
S.Ducasse 11
Metaclasses are implicit
There are no explicit metaclasses
Metaclasses are created implicitly when classes are created
No sharing of metaclasses (unique metaclass per class)
S.Ducasse 12
Metaclasses by Example
Square allSubclassesSquare allSubclasses
Snake allSubclassesSnake allSubclasses
Snake allInstancesSnake allInstances
Snake instVarNamesSnake instVarNames
Snake back: 5Snake back: 5
Snake selectorsSnake selectors
Snake canUnderstand: #newSnake canUnderstand: #new
Snake canUnderstand: #setBack:Snake canUnderstand: #setBack:
S.Ducasse 13
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the
class hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 14
4.The metaclass hierarchy parallels the
class hierarchy
S.Ducasse 15
Uniformity between Classes and Objects
Classes are objects too, so …
Everything that holds for objects holds for classes as well
Same method lookup strategy
Look up in the method dictionary of the metaclass
S.Ducasse 16
About the Buttons
S.Ducasse 17
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class
and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 18
5. Every metaclass inherits from Class and
Behavior
18
Every class is-a Class =
The metaclass of every
class inherits from Class
S.Ducasse 19
Where is new defined?
S.Ducasse 20
Responsibilities of Behavior
Behavior
Minimum state necessary for objects that have instances.
Basic interface to the compiler.
State:
class hierarchy link, method dictionary, description of instances
(representation and number)
Methods:
creating a method dictionary, compiling method
instance creation (new, basicNew, new:, basicNew:)
class hierarchy manipulation (superclass:, addSubclass:)
accessing (selectors, allSelectors, compiledMethodAt: )
accessing instances and variables (allInstances, instVarNames)
accessing class hierarchy (superclass, subclasses)
testing (hasMethods, includesSelector, canUnderstand:,
inheritsFrom:, isVariable)
S.Ducasse 21
Responsibilities of ClassDescription
ClassDescription
adds a number of facilities to basic Behavior:
named instance variables
category organization for methods
the notion of a name (abstract)
maintenance of Change sets and logging changes
most of the mechanisms needed for fileOut
ClassDescription is an abstract class: its facilities are
intended for inheritance by the two subclasses, Class and
Metaclass.
S.Ducasse 22
Responsibilities of Class
Class
represents the common behavior of all classes
name, compilation, method storing, instance variables …
representation for classVariable names and shared pool
variables (addClassVarName:, addSharedPool:,
initialize)
Class inherits from Object because Class is an
Object
Class knows how to create instances, so all metaclasses should
inherit ultimately from Class
S.Ducasse 23
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of
Metaclass
The metaclass of Metaclass is an instance of
Metaclass
S.Ducasse 24
6. Every metaclass is an instance of
Metaclass
S.Ducasse 25
Metaclass Responsibilities
Metaclass
Represents common metaclass Behavior
instance creation (subclassOf:)
creating initialized instances of the metaclass’s sole instance
initialization of class variables
metaclass instance protocol (name:inEnvironment:subclassOf:....)
method compilation (different semantics can be introduced)
class information (inheritance link, instance variable, ...)
S.Ducasse 26
Metaclasses in 7 points
Every object is an instance of a class
Every class eventually inherits from Object
Every class is an instance of a metaclass
The metaclass hierarchy parallels the class
hierarchy
Every metaclass inherits from Class and Behavior
Every metaclass is an instance of Metaclass
The metaclass of Metaclass is an
instance of Metaclass
S.Ducasse 27
7.The metaclass of Metaclass is an instance
of Metaclass
S.Ducasse 28
Navigating the metaclass hierarchy
MetaclassHierarchyTest>>testHierarchyMetaclassHierarchyTest>>testHierarchy
"The class hierarchy""The class hierarchy"
self assert: Snake superclass = Square.self assert: Snake superclass = Square.
self assert: Square superclass = Object.self assert: Square superclass = Object.
self assert: Object superclass superclass = nil. "skip ProtoObject"self assert: Object superclass superclass = nil. "skip ProtoObject"
"The parallel metaclass hierarchy""The parallel metaclass hierarchy"
self assert: Snake class name = 'Snake class'.self assert: Snake class name = 'Snake class'.
self assert: Snake class superclass = Square class.self assert: Snake class superclass = Square class.
self assert: Square class superclass = Object class.self assert: Square class superclass = Object class.
self assert: Object class superclass superclass = Class.self assert: Object class superclass superclass = Class.
self assert: Class superclass = ClassDescription.self assert: Class superclass = ClassDescription.
self assert: ClassDescription superclass = Behavior.self assert: ClassDescription superclass = Behavior.
self assert: Behavior superclass = Object.self assert: Behavior superclass = Object.
"The Metaclass hierarchy""The Metaclass hierarchy"
self assert: Snake class class = Metaclass.self assert: Snake class class = Metaclass.
self assert: Square class class = Metaclass.self assert: Square class class = Metaclass.
self assert: Object class class = Metaclass.self assert: Object class class = Metaclass.
self assert: Class class class = Metaclass.self assert: Class class class = Metaclass.
self assert: ClassDescription class class = Metaclass.self assert: ClassDescription class class = Metaclass.
self assert: Behavior class class = Metaclass.self assert: Behavior class class = Metaclass.
self assert: Metaclass superclass = ClassDescription.self assert: Metaclass superclass = ClassDescription.
"The fixpoint""The fixpoint"
self assert: Metaclass class class = Metaclass.self assert: Metaclass class class = Metaclass.

More Related Content

What's hot (6)

Javabeanproperties
JavabeanpropertiesJavabeanproperties
Javabeanproperties
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
Objective runtime
Objective runtimeObjective runtime
Objective runtime
 
Uniform and Safe Metaclass Composition
Uniform and Safe Metaclass CompositionUniform and Safe Metaclass Composition
Uniform and Safe Metaclass Composition
 
Calypso browser
Calypso browserCalypso browser
Calypso browser
 

Viewers also liked (20)

Stoop 423-some designpatterns
Stoop 423-some designpatternsStoop 423-some designpatterns
Stoop 423-some designpatterns
 
10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)10 - OOP - Inheritance (b)
10 - OOP - Inheritance (b)
 
11 - OOP - Numbers
11 - OOP - Numbers11 - OOP - Numbers
11 - OOP - Numbers
 
Stoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesignStoop 414-smalltalk elementsofdesign
Stoop 414-smalltalk elementsofdesign
 
12 virtualmachine
12 virtualmachine12 virtualmachine
12 virtualmachine
 
Stoop 433-chain
Stoop 433-chainStoop 433-chain
Stoop 433-chain
 
09 metaclasses
09 metaclasses09 metaclasses
09 metaclasses
 
08 refactoring
08 refactoring08 refactoring
08 refactoring
 
03 standardclasses
03 standardclasses03 standardclasses
03 standardclasses
 
Stoop 415-design points
Stoop 415-design pointsStoop 415-design points
Stoop 415-design points
 
Stoop 400-metaclass only
Stoop 400-metaclass onlyStoop 400-metaclass only
Stoop 400-metaclass only
 
Stoop 421-design heuristics
Stoop 421-design heuristicsStoop 421-design heuristics
Stoop 421-design heuristics
 
9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)9 - OOP - Smalltalk Classes (b)
9 - OOP - Smalltalk Classes (b)
 
Stoop ed-some principles
Stoop ed-some principlesStoop ed-some principles
Stoop ed-some principles
 
Stoop sed-sharing ornot
Stoop sed-sharing ornotStoop sed-sharing ornot
Stoop sed-sharing ornot
 
Stoop 450-s unit
Stoop 450-s unitStoop 450-s unit
Stoop 450-s unit
 
10 reflection
10 reflection10 reflection
10 reflection
 
07 bestpractice
07 bestpractice07 bestpractice
07 bestpractice
 
Stoop 434-composite
Stoop 434-compositeStoop 434-composite
Stoop 434-composite
 
Stoop ed-inheritance composition
Stoop ed-inheritance compositionStoop ed-inheritance composition
Stoop ed-inheritance composition
 

Similar to Stoop 400 o-metaclassonly

9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Inheritance And Traits
Inheritance And TraitsInheritance And Traits
Inheritance And Traits
Piyush Mishra
 

Similar to Stoop 400 o-metaclassonly (20)

Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Chap11
Chap11Chap11
Chap11
 
ACM init() Day 6
ACM init() Day 6ACM init() Day 6
ACM init() Day 6
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Chapter 9 java
Chapter 9 javaChapter 9 java
Chapter 9 java
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Java session2
Java session2Java session2
Java session2
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Oops
OopsOops
Oops
 
Object oriented programming tutorial
Object oriented programming tutorialObject oriented programming tutorial
Object oriented programming tutorial
 
Pharo Hands-on: 05 object model
Pharo Hands-on: 05 object modelPharo Hands-on: 05 object model
Pharo Hands-on: 05 object model
 
Class notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritanceClass notes(week 6) on inheritance and multiple inheritance
Class notes(week 6) on inheritance and multiple inheritance
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Inheritance And Traits
Inheritance And TraitsInheritance And Traits
Inheritance And Traits
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
 
LISP:Object System Lisp
LISP:Object System LispLISP:Object System Lisp
LISP:Object System Lisp
 

More from The World of Smalltalk (19)

05 seaside canvas
05 seaside canvas05 seaside canvas
05 seaside canvas
 
99 questions
99 questions99 questions
99 questions
 
13 traits
13 traits13 traits
13 traits
 
11 bytecode
11 bytecode11 bytecode
11 bytecode
 
06 debugging
06 debugging06 debugging
06 debugging
 
05 seaside
05 seaside05 seaside
05 seaside
 
04 idioms
04 idioms04 idioms
04 idioms
 
02 basics
02 basics02 basics
02 basics
 
01 intro
01 intro01 intro
01 intro
 
Stoop sed-smells
Stoop sed-smellsStoop sed-smells
Stoop sed-smells
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop sed-class initialization
Stoop sed-class initializationStoop sed-class initialization
Stoop sed-class initialization
 
Stoop ed-unit ofreuse
Stoop ed-unit ofreuseStoop ed-unit ofreuse
Stoop ed-unit ofreuse
 
Stoop ed-subtyping subclassing
Stoop ed-subtyping subclassingStoop ed-subtyping subclassing
Stoop ed-subtyping subclassing
 
Stoop ed-lod
Stoop ed-lodStoop ed-lod
Stoop ed-lod
 
Stoop ed-frameworks
Stoop ed-frameworksStoop ed-frameworks
Stoop ed-frameworks
 
Stoop ed-dual interface
Stoop ed-dual interfaceStoop ed-dual interface
Stoop ed-dual interface
 
Stoop ed-class forreuse
Stoop ed-class forreuseStoop ed-class forreuse
Stoop ed-class forreuse
 
Stoop 440-adaptor
Stoop 440-adaptorStoop 440-adaptor
Stoop 440-adaptor
 

Stoop 400 o-metaclassonly

  • 1. S.Ducasse 1 Metaclasses in 7 Steps Classes are objects too... Classes are instances of other classes ... One model applied twice
  • 2. S.Ducasse 2 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass Adapted from Goldberg & Robson, Smalltalk-80 — The Language
  • 3. S.Ducasse 3 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 4. S.Ducasse 4 1. Every object is an instance of a class
  • 5. S.Ducasse 5 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 6. S.Ducasse 6 2. Every class inherits from Object Every object is-an Object The class of every object ultimately inherits from Object
  • 7. S.Ducasse 7 The Meaning of is-a When an object receives a message, the method is looked up in the method dictionary of its class, and, if necessary, its superclasses, up to Object
  • 8. S.Ducasse 8 Responsibilities of Object Object represents the common object behavior error-handling, halting … all classes should inherit ultimately from Object
  • 9. S.Ducasse 9 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 10. S.Ducasse 10 3. Every class is an instance of a metaclass Classes are objects too! Every class X is the unique instance of its metaclass, called X class
  • 11. S.Ducasse 11 Metaclasses are implicit There are no explicit metaclasses Metaclasses are created implicitly when classes are created No sharing of metaclasses (unique metaclass per class)
  • 12. S.Ducasse 12 Metaclasses by Example Square allSubclassesSquare allSubclasses Snake allSubclassesSnake allSubclasses Snake allInstancesSnake allInstances Snake instVarNamesSnake instVarNames Snake back: 5Snake back: 5 Snake selectorsSnake selectors Snake canUnderstand: #newSnake canUnderstand: #new Snake canUnderstand: #setBack:Snake canUnderstand: #setBack:
  • 13. S.Ducasse 13 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 14. S.Ducasse 14 4.The metaclass hierarchy parallels the class hierarchy
  • 15. S.Ducasse 15 Uniformity between Classes and Objects Classes are objects too, so … Everything that holds for objects holds for classes as well Same method lookup strategy Look up in the method dictionary of the metaclass
  • 17. S.Ducasse 17 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 18. S.Ducasse 18 5. Every metaclass inherits from Class and Behavior 18 Every class is-a Class = The metaclass of every class inherits from Class
  • 19. S.Ducasse 19 Where is new defined?
  • 20. S.Ducasse 20 Responsibilities of Behavior Behavior Minimum state necessary for objects that have instances. Basic interface to the compiler. State: class hierarchy link, method dictionary, description of instances (representation and number) Methods: creating a method dictionary, compiling method instance creation (new, basicNew, new:, basicNew:) class hierarchy manipulation (superclass:, addSubclass:) accessing (selectors, allSelectors, compiledMethodAt: ) accessing instances and variables (allInstances, instVarNames) accessing class hierarchy (superclass, subclasses) testing (hasMethods, includesSelector, canUnderstand:, inheritsFrom:, isVariable)
  • 21. S.Ducasse 21 Responsibilities of ClassDescription ClassDescription adds a number of facilities to basic Behavior: named instance variables category organization for methods the notion of a name (abstract) maintenance of Change sets and logging changes most of the mechanisms needed for fileOut ClassDescription is an abstract class: its facilities are intended for inheritance by the two subclasses, Class and Metaclass.
  • 22. S.Ducasse 22 Responsibilities of Class Class represents the common behavior of all classes name, compilation, method storing, instance variables … representation for classVariable names and shared pool variables (addClassVarName:, addSharedPool:, initialize) Class inherits from Object because Class is an Object Class knows how to create instances, so all metaclasses should inherit ultimately from Class
  • 23. S.Ducasse 23 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 24. S.Ducasse 24 6. Every metaclass is an instance of Metaclass
  • 25. S.Ducasse 25 Metaclass Responsibilities Metaclass Represents common metaclass Behavior instance creation (subclassOf:) creating initialized instances of the metaclass’s sole instance initialization of class variables metaclass instance protocol (name:inEnvironment:subclassOf:....) method compilation (different semantics can be introduced) class information (inheritance link, instance variable, ...)
  • 26. S.Ducasse 26 Metaclasses in 7 points Every object is an instance of a class Every class eventually inherits from Object Every class is an instance of a metaclass The metaclass hierarchy parallels the class hierarchy Every metaclass inherits from Class and Behavior Every metaclass is an instance of Metaclass The metaclass of Metaclass is an instance of Metaclass
  • 27. S.Ducasse 27 7.The metaclass of Metaclass is an instance of Metaclass
  • 28. S.Ducasse 28 Navigating the metaclass hierarchy MetaclassHierarchyTest>>testHierarchyMetaclassHierarchyTest>>testHierarchy "The class hierarchy""The class hierarchy" self assert: Snake superclass = Square.self assert: Snake superclass = Square. self assert: Square superclass = Object.self assert: Square superclass = Object. self assert: Object superclass superclass = nil. "skip ProtoObject"self assert: Object superclass superclass = nil. "skip ProtoObject" "The parallel metaclass hierarchy""The parallel metaclass hierarchy" self assert: Snake class name = 'Snake class'.self assert: Snake class name = 'Snake class'. self assert: Snake class superclass = Square class.self assert: Snake class superclass = Square class. self assert: Square class superclass = Object class.self assert: Square class superclass = Object class. self assert: Object class superclass superclass = Class.self assert: Object class superclass superclass = Class. self assert: Class superclass = ClassDescription.self assert: Class superclass = ClassDescription. self assert: ClassDescription superclass = Behavior.self assert: ClassDescription superclass = Behavior. self assert: Behavior superclass = Object.self assert: Behavior superclass = Object. "The Metaclass hierarchy""The Metaclass hierarchy" self assert: Snake class class = Metaclass.self assert: Snake class class = Metaclass. self assert: Square class class = Metaclass.self assert: Square class class = Metaclass. self assert: Object class class = Metaclass.self assert: Object class class = Metaclass. self assert: Class class class = Metaclass.self assert: Class class class = Metaclass. self assert: ClassDescription class class = Metaclass.self assert: ClassDescription class class = Metaclass. self assert: Behavior class class = Metaclass.self assert: Behavior class class = Metaclass. self assert: Metaclass superclass = ClassDescription.self assert: Metaclass superclass = ClassDescription. "The fixpoint""The fixpoint" self assert: Metaclass class class = Metaclass.self assert: Metaclass class class = Metaclass.