GEOS SDK TechDocs
|
|
3.1 Objects, Messages, and Methods
|
4 Multitasking and Multithreading
If every object had to be coded and debugged once for each time it was used, OOP would provide few benefits over standard procedural programming. Many objects share similar functionality and could easily make use of the same code over and over again. The concepts of class and inheritance allow objects with similar data structures and methods to use common code.
The class is actually where the functionality of objects is defined. Every object is simply an
instance
of a class, a manifestation of the instance data and methods defined for the class. For example, the Counter object of Objects, Messages, and Methods
could be an instance of a class called
CounterClass
. Other counters sharing the same characteristics would also be instances of
CounterClass
, each having its own value in its own instance data field.
A main benefit of the implementation of classes in GEOS is that objects can be created and destroyed during execution. Class definitions are stored separately from an individual instance's data; each instance knows where its class is located. Therefore, if two instances of a particular class exist, they both point to a single copy of the class definition. Since the code is stored in the class definition, the instances can use up only as much memory as their data fields require; each instance does not need to store its own method code.
Another, more powerful, benefit is gained through the implementation of inheritance. In many cases, objects will be similar but not identical. This means that their classes are also similar but not identical. Inheritance overcomes this problem through the use of superclasses and subclasses .
A class provides a certain set of instance data and methods. A subclass inherits all the instance data structures and methods of its superclass, modifying or enhancing them to provide some different functionality.
Take, for example, the class
CounterClass
. An instance of this class would provide a counter with certain features: The counter goes from zero to 100, it can be reset manually, it resets automatically when it reaches 100, and its value is retrieved with the use of a certain message. However, suppose we need a counter that does all those things
and
allows the counter to be set to an arbitrary value with the use of a new message.
Without inheritance, we would have to write all the code for
CounterClass
over again as a new class, adding the new method. However, by creating a subclass of
CounterClass
(let's call it
CounterSetClass
), we can inherit all the methods and data structures of
CounterClass
without recoding them. Instead, we simply add the new message and the code for the new method. (It is also possible to modify inherited methods in a subclass.) This example of
CounterClass
and
CounterSetClass
is shown in the figure above.
The inheritance implemented in GEOS is actually much more complex and sophisticated than this simple example shows. You may never need to know more than the above concepts, however.
GEOS classes may be of a special type known as a variant . Variant classes do not initially know what their superclass is; instead, the superclass is determined by context for each instance of the variant class. This is a complicated topic that is discussed later in the documentation.
GEOS SDK TechDocs
|
|
3.1 Objects, Messages, and Methods
|
4 Multitasking and Multithreading