2.3. The Advantages of Object Oriented Programming

Object oriented programming (OOP)is well suited to describe autonomous agents,so it should have appeal to scientists and modelers on that basis alone. However, that is not the end of the subject. OOP it has virtues that are equally important to computer programmers. OOP, as it is found in Objective-C, is not exactly the same as OOP in C++ or Java, but these languages have some significant features in common. The features we emphasize here are encapsulation and inheritance.

2.3.1. Encapsulation

This has both substantive and practical implications. The substantive importance is that the representation of an individual actor now presumes that the actor is a self-contained entity and that other actors do not automatically have access to all information inside that actor. Like humans, objects have to take effort to convey information to each other about their internal states. The practical advantages of encapsulation, however, are just as important. Computer projects can be broken down into separable components (code for the classes) and when the code is finished, the details of what goes on inside each object may not be important to the programmer. For example, if an object groceryStore can respond to an message takeMoney, and it gets the job done, we might not care how it does it.

Figure 2-2. Interface vs. Implementation

This is commonly referred to as the separation of "interface" from "implementation." While the interface declares what methods the object can execute, the implementation may remain hidden (see Figure 2-2), the user only has to be familiar with the interface of an object, not it's implementation

2.3.2. Inheritance

Inheritance works because code for each class designates that class as a subclass of a superclass. For example, in the GNU Objective-C compiler used in the Swarm project, there is a most basic class, "object". From the object class, the Swarm libraries create subclasses, and subclasses are created from them, and so forth until the programmer in a swarm project wants to create a new class of actors that is subclassed from SwarmObject. If the programmer needs to create several varieties of that class, there is no need to totally rewrite each one. Subclasses can be created that have as a base all variables and methods of the class but then new methods and variables can be added as well.

When a method, say takeMoney, exists in a class Store, and then a subclass is created, say GroceryStore, then all objects instantiated from the subclass will respond to takeMoney. If the programmer wants to rewrite the takeMoney method for GroceryStores, however, then the method can be revised inside the code for the subclass and then all instances of the GroceryStore class will respond to takeMoney in that specialized way. The method inside the GroceryStore subclass will override the super-class's definition of the method.