Instructors' Guide

Introduction Terminology Expressions Control Objects Inheritance Technology Summary


Everything in Smalltalk is an object. An object may be regarded as consisting of instance variables and a collection of methods.

Object -- behavior

Class -- description

Self -- self reference

slide: Smalltalk -- objects (1)

A class is the description of a collection of objects, its instances. Considered as an object, a class may be said to have class variables and class methods.

For self-reference the special expression self may be used. To invoke methods from the parent class the expression super may be used.

An example of an object class description is given in slide sm-objects-2. The class Ctr is defined as a subclass of the class behavior. It supports an initialization protocol (containing the method initialize), a protocol for modification (containing the method add), and an inspection protocol (containing the method value).

Example -- class

  Behavior subclass: #Ctr
  instanceVariableNames: 'value'
  Ctr methodsFor: 'initialization'
  	value := 0.
  Ctr methodsFor: 'modifications'
     add: aValue
  	value := value + aValue.
  Ctr methodsFor: 'inspection'

slide: Smalltalk -- objects (2)

Note that value occurs both as an instance variable and as a method. Only the method is accessible by the user.

In addition, we need a class description defining the object functionality of Ctr, which consists of an {\em instance creation} protocol defining the class method new. See slide sm-objects-3. This class description is (implicitly) an instance of a meta class generated by the Smalltalk system. See section meta.

Class description -- meta class

  Ctr class
  instanceVariableNames: ''
  Ctr class methodsFor: 'instance creation'
  		^super new initialize

slide: Smalltalk -- objects (3)