SlideShare a Scribd company logo
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

PPT
Stoop metaclasses
The World of Smalltalk
 
PPT
Stoop 304-metaclasses
The World of Smalltalk
 
PDF
Metaclass Programming in Python
Juan-Manuel Gimeno
 
PDF
Reflection and Introspection
adil raja
 
PDF
javainheritance
Arjun Shanka
 
KEY
Metaprogramming Primer (Part 1)
Christopher Haupt
 
PPTX
Java Inheritance | Java Course
RAKESH P
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
Stoop metaclasses
The World of Smalltalk
 
Stoop 304-metaclasses
The World of Smalltalk
 
Metaclass Programming in Python
Juan-Manuel Gimeno
 
Reflection and Introspection
adil raja
 
javainheritance
Arjun Shanka
 
Metaprogramming Primer (Part 1)
Christopher Haupt
 
Java Inheritance | Java Course
RAKESH P
 
Unit3 part2-inheritance
DevaKumari Vijay
 

What's hot (6)

PPTX
Javabeanproperties
vamsitricks
 
PPT
04 inheritance
Pondugala Sowjanya
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PDF
Objective runtime
Michael Shieh
 
PDF
Uniform and Safe Metaclass Composition
ESUG
 
PDF
Calypso browser
Denis Kudryashov
 
Javabeanproperties
vamsitricks
 
04 inheritance
Pondugala Sowjanya
 
Unit3 inheritance
Kalai Selvi
 
Objective runtime
Michael Shieh
 
Uniform and Safe Metaclass Composition
ESUG
 
Calypso browser
Denis Kudryashov
 
Ad

Viewers also liked (20)

PPT
Stoop 423-some designpatterns
The World of Smalltalk
 
PPT
10 - OOP - Inheritance (b)
The World of Smalltalk
 
PPT
11 - OOP - Numbers
The World of Smalltalk
 
PPT
Stoop 414-smalltalk elementsofdesign
The World of Smalltalk
 
PPT
12 virtualmachine
The World of Smalltalk
 
PPT
Stoop 433-chain
The World of Smalltalk
 
PPT
09 metaclasses
The World of Smalltalk
 
PPT
08 refactoring
The World of Smalltalk
 
PPT
03 standardclasses
The World of Smalltalk
 
PDF
Stoop 415-design points
The World of Smalltalk
 
PDF
Stoop 400-metaclass only
The World of Smalltalk
 
PPT
Stoop 421-design heuristics
The World of Smalltalk
 
PPT
9 - OOP - Smalltalk Classes (b)
The World of Smalltalk
 
PPT
Stoop ed-some principles
The World of Smalltalk
 
PPT
Stoop sed-sharing ornot
The World of Smalltalk
 
PPT
Stoop 450-s unit
The World of Smalltalk
 
PPT
10 reflection
The World of Smalltalk
 
PPT
07 bestpractice
The World of Smalltalk
 
PPT
Stoop 434-composite
The World of Smalltalk
 
PPT
Stoop ed-inheritance composition
The World of Smalltalk
 
Stoop 423-some designpatterns
The World of Smalltalk
 
10 - OOP - Inheritance (b)
The World of Smalltalk
 
11 - OOP - Numbers
The World of Smalltalk
 
Stoop 414-smalltalk elementsofdesign
The World of Smalltalk
 
12 virtualmachine
The World of Smalltalk
 
Stoop 433-chain
The World of Smalltalk
 
09 metaclasses
The World of Smalltalk
 
08 refactoring
The World of Smalltalk
 
03 standardclasses
The World of Smalltalk
 
Stoop 415-design points
The World of Smalltalk
 
Stoop 400-metaclass only
The World of Smalltalk
 
Stoop 421-design heuristics
The World of Smalltalk
 
9 - OOP - Smalltalk Classes (b)
The World of Smalltalk
 
Stoop ed-some principles
The World of Smalltalk
 
Stoop sed-sharing ornot
The World of Smalltalk
 
Stoop 450-s unit
The World of Smalltalk
 
10 reflection
The World of Smalltalk
 
07 bestpractice
The World of Smalltalk
 
Stoop 434-composite
The World of Smalltalk
 
Stoop ed-inheritance composition
The World of Smalltalk
 
Ad

Similar to Stoop 400 o-metaclassonly (20)

PPT
Stoop 305-reflective programming5
The World of Smalltalk
 
PDF
L5
lksoo
 
PPT
Polymorphism
Kumar
 
PPT
10 - OOP - Inheritance (a)
The World of Smalltalk
 
PPT
Object and Classes in Java
backdoor
 
PPT
Stoop ed-subtyping subclassing
The World of Smalltalk
 
PPT
7 - OOP - OO Concepts
The World of Smalltalk
 
PPT
Object -oriented analysis and design.ppt
pierrerj05
 
PPT
9 - OOP - Smalltalk Classes (a)
The World of Smalltalk
 
PPT
9 - OOP - Smalltalk Classes (c)
The World of Smalltalk
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPT
Stoop 422-naming idioms
The World of Smalltalk
 
KEY
Ruby's metaclass
xds2000
 
PPTX
01. design pattern
MD Sayem Ahmed
 
PDF
Pharo Hands-on: 05 object model
Pharo
 
PPT
8 polymorphism
Abhijit Gaikwad
 
PDF
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
PPTX
OOP Presentation.pptx
DurgaPrasadVasantati
 
Stoop 305-reflective programming5
The World of Smalltalk
 
L5
lksoo
 
Polymorphism
Kumar
 
10 - OOP - Inheritance (a)
The World of Smalltalk
 
Object and Classes in Java
backdoor
 
Stoop ed-subtyping subclassing
The World of Smalltalk
 
7 - OOP - OO Concepts
The World of Smalltalk
 
Object -oriented analysis and design.ppt
pierrerj05
 
9 - OOP - Smalltalk Classes (a)
The World of Smalltalk
 
9 - OOP - Smalltalk Classes (c)
The World of Smalltalk
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Stoop 422-naming idioms
The World of Smalltalk
 
Ruby's metaclass
xds2000
 
01. design pattern
MD Sayem Ahmed
 
Pharo Hands-on: 05 object model
Pharo
 
8 polymorphism
Abhijit Gaikwad
 
Ruby object model at the Ruby drink-up of Sophia, January 2013
rivierarb
 
OOP Presentation.pptx
DurgaPrasadVasantati
 

More from The World of Smalltalk (18)

PDF
05 seaside canvas
The World of Smalltalk
 
PPT
99 questions
The World of Smalltalk
 
PPT
11 bytecode
The World of Smalltalk
 
PPT
06 debugging
The World of Smalltalk
 
PPT
05 seaside
The World of Smalltalk
 
PPT
Stoop sed-smells
The World of Smalltalk
 
PPT
Stoop sed-class initialization
The World of Smalltalk
 
PPT
Stoop sed-class initialization
The World of Smalltalk
 
PPT
Stoop ed-unit ofreuse
The World of Smalltalk
 
PPT
Stoop ed-lod
The World of Smalltalk
 
PPT
Stoop ed-frameworks
The World of Smalltalk
 
PPT
Stoop ed-dual interface
The World of Smalltalk
 
PPT
Stoop ed-class forreuse
The World of Smalltalk
 
PPT
Stoop 440-adaptor
The World of Smalltalk
 
05 seaside canvas
The World of Smalltalk
 
Stoop sed-smells
The World of Smalltalk
 
Stoop sed-class initialization
The World of Smalltalk
 
Stoop sed-class initialization
The World of Smalltalk
 
Stoop ed-unit ofreuse
The World of Smalltalk
 
Stoop ed-frameworks
The World of Smalltalk
 
Stoop ed-dual interface
The World of Smalltalk
 
Stoop ed-class forreuse
The World of Smalltalk
 
Stoop 440-adaptor
The World of Smalltalk
 

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.