Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


17.1.1 Classes and methods

The heart of OOP is the `Black Box' approach to programming. The kind of black box in question is one where the contents are unknown but there is a number of wires on the outside which give you some way of interacting with the stuff on the inside. The black boxes of OOP are actually collections of data (just like the idea of variables that we've already met) and they are called objects (this is the general term, which is, coincidentally, connected with the OBJECT type in E). Objects can be grouped together in classes, like the types for variables, except that a class also defines what different kinds of wires protrude from the black box. This extra bit (the wires) is known as the interface to the object, and is made up of a number of methods (so a method is analogous to a wire). Each method is actually just like a procedure. With a real black box, the wires are the only way of interacting with the box, so the methods of an object ought to be the only way of creating and using the object. Of course, the methods themselves normally need to know the internal workings of the object, just like the way the wires are normally connected to something inside the black box.

There are two special kinds of methods: constructors and destructors. A constructor is a method which is used to initialise the data in an object, and a class may have several different constructors (allowing for different kinds of initialisation) or it may have none if no special initialisation is necessary. Constructors are normally used to allocate the resources (such as memory) that an object needs. The deallocation of such resources is done by the destructor, of which there is at most one for each class.

Protecting the contents of an object in the `black box' way is known as data-hiding (the data in the object is visible only to its methods), and only allowing the contents of an object to be manipulated via its interface is known as data abstraction. By using this approach, only the methods know the structure of the data in an object and so this structure can be changed without affecting the whole of a program: only the methods would potentially need recoding. As you might be able to tell, this simplifies maintenance quite considerably.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.