Previous | Table of Contents | Next |
Using this method, a new Customer object might be created and assigned to a temporary variable by this expression:
temp := Customer new name:Joe address:Any City customerNumber: 1.
This expression first sends a message with the selector new to the class object named Customer. The result of that message send is a new instance of Customer, which becomes the receiver for the message with the selector name:address:customerNumber:. This message invokes the method that assigns the arguments to the receivers instance variables and then returns the receiver. Finally, the new customer object, which is the returned value, is assigned to the variable named temp.
Users of the Customer class are likely to want to retrieve the values stored in its instance variables. Because of the encapsulation of instance variables, this requires the use of methods such as the following:
name return the name of the customer ^name address return the current address from the receiver ^address
Methods such as these that simply return the value of an instance variable are called accessor methods. Note that a programmer need not provide accessor methods for all the instance variables defined by a class. Some instance variables can be considered completely private to the implementation of the class. If an accessor method is not provided for such an instance variable, it is accessible only to the classs methods.
Instead of thinking about accessor methods as retrieving the value of an instance variable, it is better to think about them as retrieving the value of an abstract attribute of the object. The implementation of this attribute might initially be as an instance variable, but as the definition of the class evolves, the implementation of the attribute might change to become some sort of computation. The use of accessor methods shields client code from such changes. Smalltalks uniformity of reference greatly facilitates this type of abstraction. All accesses to an object, outside of the objects class definition, must be expressed as message sends.
Now that we have defined some methods for Customer, we can complete its class definition template:
Class Name | Customer |
Superclass | Object |
Instance Variables | name address customerNumber |
Instance Methods | |
name: initName address: initAddr customerNumber: initNumber initialize the state of the receiver to the argument values name := initName. Address := initAddr. customerNumber := initNumber | |
name return the name of the customer ^name | |
address return the current address from the receiver ^address |
The one part of the basic class definition template that remains to be discussed is the superclass. Most classes are defined as refinements of an existing class. The class from which a new class is derived is called the superclass of the new class. The derived class is called a subclass of its superclass.
A class can have exactly one immediate superclass. The superclass field of a class definition is used to specify the immediate superclass. The immediate superclass can itself be a subclass of another class. Thus, each class can have a chain of superclasses that starts with its immediate superclass and continues through the superclasses of the immediate superclass. The final class of this chain is usually the class named Object. Object does not have a superclass.
The relationship between a class and its superclasses is called inheritance. A subclass inherits the instance variable and method definitions of its superclasses. Because Smalltalk only allows an object to have a single immediate superclass, it is said to support single inheritance. This is in contrast to languages such as C++ and Eiffel that allow a class to have multiple immediate superclasses. Such languages are said to support multiple inheritance.
Through the mechanisms of inheritance, all the instance variables and methods defined by the superclasses are also defined for the subclass. The instance variables and methods explicitly defined by the subclass augment those that are inherited from its superclasses.
The encapsulated state of an instance of a class includes instance variables corresponding to each of the instance variables of its superclasses in addition to the instance variables it explicitly defines. The names of each instance variable must be unique for the class, regardless of whether the variable is defined by the class or by one of its superclasses.
The behavior of an instance of a subclass includes all the instance methods defined by its superclasses, augmented by the methods explicitly defined in the subclass definition. If the selector of a method in a subclass is the same as the selector of a method that is inherited from a superclass, the subclasss method overrides (replaces) the inherited method in the subclasss behavior. Thus, a subclass can extend the inherited behavior of its superclass by defining new methods or can modify the inherited behavior by overriding methods. Smalltalk does not provide a mechanism for excluding an inherited method from the behavior of a subclass.
Previous | Table of Contents | Next |