| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 12)

Introduction to Methods

In G2's object-oriented programming language:

Methods allow you to customize operations in class-specific ways. In conjunction with class hierarchy, methods allow you to define item behavior with great economy and modularity. Two essential capabilities provide these advantages:

Methods and Procedures

Methods are syntactically and functionally similar to procedures, and procedures can do anything that methods can do. However, methods are typically more convenient than procedures for defining complex behavior, because they are more modular, flexible, maintainable, and reusable.

G2 methods have essentially the same syntax as ordinary G2 procedures. Both procedures and methods:

The only syntactic differences are:

Thus any procedure that has the first property listed could be used as a method without changing its code in any way. The essential difference between procedures and methods is not in their code, but in the way G2 invokes them.

The Vessel Example

A simple example can demonstrate all the essential features of methods, and show you how they compare with procedures. Suppose that:

The rest of this chapter uses this example at various points to illustrate the properties of methods.

Filling Vessels Using Procedures

To fill vessels by using procedures, you could create a procedure for each of the four classes. For example:

fill-vessel (V: class vessel)
Fill the vessel (tank, bottle, or flask).
fill-tank (T: class tank)
Unscrew the tank's cap, invoke fill-vessel to fill the tank, then replace the tank's cap.
fill-bottle (B: class bottle)
Remove the bottle's cork, invoke fill-vessel to fill the bottle, then replace the bottle's cork.
fill-flask (F: class flask)
Sterilize the flask, then invoke fill-vessel to fill the flask.

Your code would need to know in advance which class of vessel is to be filled, and invoke a different procedure depending on the class, or else use a case statement that selects on class to choose the correct procedure dynamically. The former technique greatly constricts code flexibility. The latter is not too burdensome for three subclasses - but what if there were hundreds of them?

Filling Vessels Using Methods

To fill vessels by using methods, you could create a method for each of the four classes. Each of these methods would be similar to the analogous procedure, with the following differences.

For example:

fill (V: class vessel)
Fill the vessel (tank, bottle, or flask).
fill (T: class tank)
Unscrew the tank's cap, execute call next method to fill the tank, then replace the tank's cap.
fill (B: class bottle)
Remove the bottle's cork, execute call next method to fill the bottle, then replace the bottle's cork.
fill (F: class flask)
Sterilize the flask, then execute call next method to fill the flask.

Your code would not need to know the class of a vessel to be filled, or use a case statement that selects on class. With the above methods defined, you can invoke fill on any tank, bottle, or flask. G2 then looks at the class of the vessel and invokes the fill method specific to that class. Thus fill means different things for different classes. This property of methods is called polymorphism.

When the method G2 selected executes call next method, G2 scans the class hierarchy path of the relevant class, looking for a superior class that also has a fill method. The class vessel is the direct superior of tank, bottle, and flask, and defines a fill method. G2 invokes that method on the vessel. When the method returns, the lower-level method continues execution.

Encapsulation

Methods allow existing code to be extended more easily than procedures do. Suppose that you now define a fourth subclass of vessel, say tube, which must be washed before it can be filled, and labeled afterwards. You need only define a fill method bound to tube, and code that method to:

Existing code already used to fill tanks, bottles, and flasks, can now fill tubes also, yet the code itself has not changed at all. It did not need to change because the knowledge of how to fill an instance of each class resides in the class, in the form of its fill method; not in the code that calls the method, which needs to know only the operation's name. This property of methods is called encapsulation.

Duplicate Methods

On occasion, the nature of an operation requires it to do slightly different things under different circumstances to operands of the same class, and these differences require supplying the relevant method with different numbers of arguments. G2 does not provide optional arguments, but you can achieve the same effect by defining two or more methods that:

This capability allows you to customize the behavior of operations by giving different numbers of arguments when you invoke them.

Inheriting Methods

When a class defines no method for a particular operation, and a superior class does, the subclass inherits the method defined for the superior.

Suppose that vessel has another subclass, vial, that needs no preparation before filling and no cleanup afterwards. That is, filling a vial requires no customized behavior, but only the behavior characteristic of every vessel.

The vial class would need no fill method of its own. If you invoked fill on a vial, G2 would search vial's class inheritance path looking for a method named fill. The class vessel is the direct superior of vial, and defines a fill method. G2 invokes that method on the vial.

G2 does the same thing every time you invoke a method, whether directly or with call next method: it scans the class inheritance path of the relevant class, and invokes the first method it encounters that has the correct name and the right number of arguments. Since every class is the first element of its own class inheritance path, this technique gives a locally defined method precedence over any inherited method.

Defining Methods

The steps for defining a set of methods to specify behavior are:

You don't have to carry out these steps sequentially, though doing so is often convenient. The following sections give complete information on defining methods.

| Prev | Next | Start of Chapter | End of Chapter | Contents | Glossary | Index | Comments | (3 out of 12)

Copyright © 1997 Gensym Corporation, Inc.