General Rules and Purpose of Subclassing

From MorphOS Library

Revision as of 11:44, 30 December 2010 by Krashan (talk | contribs) (Contents++.)

Grzegorz Kraszewski


Introduction

Subclassing is one of essential object oriented programming techniques. In MUI subclassing is used for the following purposes:

  • Implementing program functionality as a set of methods. The Application class is usually used for this purpose.
  • Customizing classes by writing methods intentionally left unimplemented (or having some default implementations) in standard MUI classes. The most common example is the List class, but also Numeric one and others.
  • Writing custom drawn gadgets or areas. The Area class is subclassed in this case.

Regardless of the reason of subclassing, it is always done in the same way. A programmer must write new or overridden methods, create a dispatcher function, define an instance data structure (an empty one in some cases), then create the class. It is worth noting, subclassing MUI classes is done the same as subclassing BOOPSI ones. The only difference is that MUI provides own functions for class creation and disposition.


Writing Methods

A MUI method is just a plain C function, but with partially fixed prototype.

IPTR MethodName(Class *cl, Object *obj, MessageType *msg);

The method return value may be either integer or pointer to anything. That is why it uses IPTR type which has meaning of "integer big enough to hold a pointer". In current MorphOS it is just 32-bit integer (the same as LONG). Two first, fixed arguments are: pointer to the class and pointer to the object. The last one is a method message. When a method is being overridden, the type of message is determined by the superclass. For a new method, message type is defined by programmer. Some methods may have empty messages (containing only a method identifier), in this case the third argument may be ommitted.