The Smalltalk

Extending class hierarchy

Up to now I've gently described somewhat restricted programming in Smalltalk based on using built-in classes and methods only. Now it's time to start extending the hierarchy with your own classes.


Classes can have variables and methods. Variables contain the real data content and they represent so called "state of object", while methods specify reactions of the object to events from the outside world -

Variables (of some object) are accessible only by calling methods (they are private in C++ terms), while methods are always accessible from anywhere (they are public in C++ terms). In Smalltalk, there is no way to change this default behavior of variables and methods.

Variables and methods can be either instance, or static.

Instance variables
are associated with every instance of a particular class, accessible by instance methods only
Class variables
are associated only with the class itself, accessible by class methods only.

Instance methods
operate on instances and handle messages sent to them
Class methods
operate on the class itself and handle messages sent to it

Single inheritance is the only kind of inheritance available in Smalltalk. That means every class may have at most one superclass. Multiple inheritance, known for example from C++, is not supported here.

There is common superclass for all classes from the built-in hierarchy called Object.

You have basicly two possibilities, how to define your own classes:

As for the file-in format, I won't describe it here. You can find the neccessary information in documentation shipped with your Smalltalk implementation.

The main class managing tool in Smalltalk is often called System browser or Class browser

In System browser in Smalltalk/X or in some similar tool in other implementations, you can choose the position in class hierarchy, where you want to create your new class. By this decision you also say, what existing class you want to be the direct superclass of your class.

When creating your own class using the way described above, you will need to fill up a form in class browser. You should list names of all class and instance variables, you want to have in your class, on appropriate lines of that form. After that you can define your class and instance methods in appropriate list boxes of the class browser.

This way you specify something, what I call class definition. That is, class definition consists of specification of class and instance variables and the code of class and instance methods.


In methods code you can access appropriate variables from the class definition, you can use local or global variables. There are two keywords important for writting your own class and instance methods: self and super. They are meaningfull only inside class or instance methods.

is neccessary for invoking methods from the current object's class definition, where object is either class or an instance of some class

is neccessary for invoking methods from the current method's superclass definition (class definition of the superclass of the class defining the current method)

Maybe this is not too easy explanation, so look at the following examples:

       Class "A" has the following two instance methods

       ^ 1.

       ^ self test + 1.
Let's have such a Smalltalk program:
       a:= A new.     "allocates new instance of class A"
       ^ a inctest.   "return: 2"
I hope, that now it should be more clear what self is

Let's show more complex example ,for which you should be familiar with aspects of inheritance in object-oriented languages, that were not covered in this text:

Class "A" has the following class method defined
      ^ 1.

Class "B" is a sublass of "A" and  has the class methods defined

      ^ 11.

      a:= self new
      ^ a foo + a goo.

      ^ self test.

      ^ super test.

Class "C" is a subclass of "B" and has the following class methods defined

      ^ 111.

      ^ self try.
What will be the result of the following messgae expression:

^ C doItAll.

If you have allready understood the semantics of self and super, you should see that answer is: 112.