[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3. Writing New Classes

Would you add Hands-on coding examples at the marked points, and perhaps any additional pertinent examples? Francis


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1 Interface

A class interface declares instance variables, methods and the superclass name, while the implementation file holds the operational code that implements those methods. The interface is included in the source using #include:

 
#include "baseclass.h"

Typically the Interface and Implementation are held in separate files, using the .h and .m extensions, respectively. They may, however, be merged into one file, and a single file may implement many classes.

To ensure that a Header file is included only once, it is usual to protect it with pre-compiler defines:

 
#ifndef _MY_CLASS_H_INCLUDED
#define _MY_CLASS_H_INCLUDED

/* HEADER FILE */
#endif

This is the standard C technique to protect header files from being included more than once.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.1 Interface Capabilities

The interface file declares new classes that can be used by source code, holding all the information necessary to use the classes from other Objective-C code. Firstly, the file reveals to the programmer the position of the class in the class hierarchy by defining exactly which is the superclass. Secondly, it informs programmers of what variables are inherited when they create subclasses. Finally, the interface file may inform other software entities of the messages that can be sent to the class object and to the instances of the class.

The syntax of a class interface is of the form:

I would like a better example interface than this? So would you create something more real?

 
// Need something here.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.2 Including Interfaces

Source modules (including Objective-C listings and interface files) may integrate interfaces using #include. Thereafter the source module may utilize the classes in those interfaces so as to:

With the exception of the root class, all working interfaces integrate a superclass using either #include - as was seen in the previous simplified interface file example. As a result the vast majority of class files begin with a standard form that includes their superclasses, and thus places them in the class hierarchy:

Here I have another example - would you replace it with something more meaningful?

 
#include "Superclass.h"
@interface InterfaceName : InterfaceSuperclass
{
  // instance variables
}
Declared methods


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.1.3 Referring to Classes - @class

It is possible for a source module to refer to classes without including their interface files. This is useful when you just need to tell the compiler that a certain word is a class name, but you want to avoid the overhead of including the whole interface file for that class.

If you are implementing a new class, you always need to include the interface of the superclass using #include; @class cannot be used in this case because the compiler needs to know the details of the superclass and its instance variables etc., so as to create a fully working new class. If you try using @class in this case, compilation will abort.

Note. When you need to send methods to an object, it's better to include the full class interface. Even in that case - it's better *only* because it allows better type checking - which is good of course - but not essential. And - to say it all - if you use @class that way, the compiler complains as soon as you try to send a message to an object of a class declared using @class, so that you know you need to include the interface for full type checking. This means it's a good idea to always try with @class if possible and only include the interface if the compiler complains that it can't type-check without the full interface.

To inform the compiler that Border and Square are classes without including their full interface file, the following syntax is used:

@class Border, Square;

Class names may also appear in interface files at times when instance variables, return values and arguments are statically typed:

- (void) set

Another example of a class interface is required here, and perhaps an example of a class cluster would be useful as this is mentioned in chapter 1.
a simple example of a class interface <fixme: what example ?>


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2 Implementation

An interface file declares a class, while an implementation file implements it. The separation between the interface and implementation file yields a black box concept where the programmer using the class need only be concerned with the interface and its declared methods, superclasses, and instance variables. The implementation of classes is transparent to the programmer who may use them without detailed knowledge of their structures.

In the C programming language C structures are singular entities that encapsulate multiple data elements. The fields therein reside in name spaces so they do not interfere with alike named entities that are outside the structure. So the name spaces define another partition.

Such C structures relate to C functions that provide the application logic or defined mechanisms. Within a C structure data elements may be local to a function, and are protected in their name space.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.1 Writing an Implementation

An implementation file contents are encapsulated between @implementation and @end directives:

 
#include "ClassTitle .h"
@implementation ClassName :Superclass
{
  // instance variables
}
// methods
@end

The Implementation file uses #include to include a named interface file holding all declarations. Thereafter, the implementation need not declare instance variables, rather it may be dedicated to defining methods that may include a number of arguments; these are declared in the same way as the interface file but without the semicolon:

 


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.2 Referring to Instance Variables

When an instance method of a certain object is called, the method can refer to the object's instance variables directly by name.

The following example illustrates how a method definition refers to the receiving object's instance variable called line.

 
- (void) setLine: (BOOL)flag
{
  line = flag;
}

You can access instance variables of other instances of the same class by using the operator ->.

Example here.

The following code fragment demonstrates how the GPRS class may declare a statically typed object as an instance variable called qosfive:

 
@interface GPRS :NSObject
{
    Connect *qosfive;
    int frequency;
    struct features *chargeband;
}

Because qosfive is typed to the same class, the instance variables of the statically typed class are in the scope of the class. So the Connect method may set them directly:

 
-makeAnotherQosfive
{
    if (!qosfive) {
        qosfive =[[Connect alloc ] init ];
        qosfive->frequency =frequency;
        qosfive->chargeband =chargeband;
    }
    return qosfive;
}

Note. When the object is not a receiver, it is necessary that the object be statically typed in the class declaration.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.3 Instance Variable Scope

In Objective-C instance variable have three types of scope or accessibility:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.4 Accessing Object Data Structures

The instance variables or data structures of objects are accessed by sending the object messages. An Objective-C object's instance variables may be changed to a public scope by converting them into a C structure.

The @defs(className) directive produces a declaration list, where public is used as a pointer to the structure that approximates an instance of Gprschannel:

 
struct gprschannelDef {
    @defs(Gprschannel)
}*public;

An object's instance variables may be given public scope through a Gprschannel id assigned to the pointer:
 
id aGprschannel;
aWorker =[[Gprschannel alloc ] init ];
public =(struct gprschannelDef *)aGprschannel;
public->boss =nil;


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.2.5 Super and Self

In the implementation of an Objective-C method, you may use the two reserved words self and super

example here . In Objective-C some control over messaging is provided by self and super. Self searches for the method implementation in the receiving object's class.

Super begins searching for the method implementation in the superclass of the class that defines the method with super.

A method implementation may refer to the target object as self or super, where control is provided over which object performs the method.

To demonstrate the difference between self and super consider three classes:

All three classes define a method called traffic, while Two defines a method called Alert that depends on traffic.

Sending a message to the One object to invoke the Alert method, causes the Alert method to send a traffic message to the same One object.

The following source code would search for the traffic method defined in One (or self's class).

 
-Alert
{
[self traffic ];
...
}

When Two’s source code calls this object super, the messaging routine will find the version of traffic defined in Three (the superclass of Two).

 
-Alert
{
[super traffic ];
...
}

Self is a variable name that may be assigned new values particularly in definitions of class methods that often focus on class instances as opposed to class objects. For example, a method might combine allocation and initialization of an instance:

 
+(id)newRect
{
    return [[self alloc ] init ];
}

When in class methods self refers to class objects, and when in instance methods self refers to instances. self and super focus on the receiver that is the object being stimulated by a message.

Nicola/Richard add an example for self and super here - or two examples?

implementation of the example class interface


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3 Categories

A category may serve to replace a subclass; when adding methods to an existing declared class, and not to a new class, a category name is used. An interface file may declare methods under a category name that is also used in the implementation file for the definitions of those methods. Category methods are added to the class, and instances of the class will have the methods as part of their behaviour. As is the case with other methods, category methods are inherited by all the class' subclasses.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.1 Adding to Classes

A category interface has the form:

 
#include "ClassTitle .h"
@interface ClassTitle (CategoryTitle )
     declared methods
@end

The category may include a interface, so as to allow its methods to access the instance variables of named classes.

A category implementation file has the form:

 
#include "CategoryTitle .h"
@implementation ClassTitle (CategoryTitle )
    definitions of methods 
@end

Category methods may replace the conventional methods inherited by the class.

Note. A category should not be considered a substitute for a subclass.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.2 Using Categories

Categories are useful for extending existing classes, so as to add new methods to frameworks. Categories are often used to separate the implementation of a new class into a number of source files. The obvious benefits of this program development strategy include: grouping subject-oriented methods; incremental compilation for large classes; help to logically divide the class when being created by a number of developers; and, permit configuration-specific classes targeting particular applications.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by root on October, 4 2003 using texi2html