Next: , Up: Creating classes


6.4.1 Creating a new class

Guess how you create a new class? This should be getting monotonous by now—by sending a message to an object. The way we create our first “custom” class is by sending the following message:

        Object subclass: #Account.
        Account instanceVariableNames: 'balance'.

Quite a mouthful, isn't it? gnu Smalltalk provides a simpler way to write this, but for now let's stick with this. Conceptually, it isn't really that bad. The Smalltalk variable Object is bound to the grand-daddy of all classes on the system. What we're doing here is telling the Object class that we want to add to it a subclass known as Account. Then, instanceVariableNames: 'balance' tells the new class that each of its objects (instances) will have a hidden variable named balance.