Chapter 3. Nuts and Bolts of Object-Oriented Programming

Table of Contents
3.1. Multilanguage support and Swarm
3.1.1. Objective C
3.1.2. Java
3.1.3. Why is Swarm Written in Objective C?
3.2. Objective C Basics
3.2.1. The id Variable Type
3.2.2. Interface File: Declaration of a Class
3.2.3. Implementation File: Defining a Class
3.2.4. C Functions vs. Objective C Methods
3.3. Java Basics
3.4. Giving Life to Classes: Instantiation
3.4.1. Instantiation: Objective C Style
3.4.2. Instantiation: Java Style
3.5. A Brief Clarification: Classes and Protocols in Objective C

3.1. Multilanguage support and Swarm

Swarm is not a single application that is `turned on'. Swarm is a set of libraries that you can pick and choose features from. In order to use the Swarm libraries, it is necessary to create or use code that calls Swarm features.

3.1.1. Objective C

Until recently, there was one way to use Swarm features: write and compile a program in Objective C. This is a flexible and way to write a model using Swarm. Objective C models tend to have good performance because they are compiled by a native code optimizing compiler, namely GCC.

Objective C was created by Brad Cox [NeXT, 1993]. The aim was to create an elegant, object-oriented extension of C in the style of the Smalltalk language [Goldberg & Robson, 1989]. Objective C was used most intensively in the design of the NeXT computer operating system, which is now owned by Apple and is basis of Apple's runtime environment WebObjects.

3.1.2. Java

Since Swarm 2.0, modellers can use Java. For new users of Swarm, writing models in Java is considerably harder to get wrong. Java is also a more attractive languages for new users to learn since it is a popular language that has benefits outside of Swarm modelling.

Java was created by Sun Microsystems, initially to provide a platform-independent layer or interface for embedded devices (such as set-top boxes for digital television) and was originally known as Oak. With the advent and growth of the world wide web from 1992 onwards, Oak was renamed Java and redirected at the market created by the web. "Write once, run anywhere" was the motto of the original Java developers, the idea being to create an abstraction layer between the programmer and the underlying chip architecture (ix86, Sparc, Alpha) known as a virtual machine (or interpreter)[1]

The purpose of the Java layer of Swarm (actually it is a system that is potentially extensible to other languages such as Scheme or C++) is to mirror the protocols of the Swarm libraries as Java interfaces.

Note

For more details on the ongoing work of integrating Swarm with other languages and simulation technologies such as XML and Scheme, see the paper Integrating Simulation Technologies With Swarm [Daniels, 1999].

3.1.3. Why is Swarm Written in Objective C?

Since Objective C is not currently a mainstream programming language, it is natural to ask why the Swarm project chose Objective C. There are a number of reasons:

  • Objective C is easier to learn. Objective C takes the familiar C language and adds a few simple elements. Objective C does not allow overloading or multiple inheritance of classes (although the use of protocols enables this, to an extent)

  • Objective C allows run-time binding. In contrast to other languages which check the match between the receiver of a command and its ability to carry out the command when a program is compiled, Objective C leaves that matching exercise until the program is running. This means, for example, that one can have a program that builds a list of objects and then sends a message to each one. In Objective C, the compiler won't worry about which type of object is in the list. The advantage, conceptually, is that one can put commands in their code without knowing the precise identity of the receiver, so code can be written to allow an environment and set of objects to change and evolve. The disadvantage, as critics of run-time binding will quickly point out, is that programs crash when an object recieves a message and it does not have the method that it is being told to execute. The organization of Swarm into protocols reduces the risk of these crashes, however, because the compiler does check and issue a warning if a method is not implemented in a class that advertises a certain protocol.

Notes

[1]

In theory all programmers needed to do was to write so-called "pure Java" code that targeted the virtual machine, whilst the virtual machine itself only needed to ported once to each new architecture or chip, by the maintainers of the language itself (rather than the applications programmer). With certain caveats, this has been largely realized, and Java is now a robust platform that is used in both web client GUI programming (applets) as well as server applications (servlets). Use of Java does come at a cost, the extra step of translating the virtual machine instructions into the native machine instructions at run-time does have a performance penalty at run-time, but with the advent of so-called native compilers which do this translation at compile-time (rather than at run-time), many applications can, in principle, run much faster than they currently do.