Objective-C is an object-oriented extension to the C language. It is C with a small number of extensions. Although the differences can fade into shades of grey, Objective-C is different from C++. C++ is traditionally associated with the Simula 67 school of object-oriented programming where Objective-C has SmallTalk roots. In C++, the static type of an object determines whether it can receive a message. In Objective-C the dynamic type of an object determines whether it can receive a message. The Simula 67 format is more structured and allows problems to be detected earlier when a program is compiled. The Smalltalk approach delays it's typing until runtime and is touted as a more flexible alternative. This flexibility extends the language into three separate areas: Dynamic Typing, Dynamic Binding and Dynamic Loading.

Dynamic Typing
As opposed to other languages, Objective-C delays the typing of language objects until a program is executed at run time. The method name, type and argument information as well as class variable instance information are available to provide the essential support for Dynamic Binding. This might by likened to the System Object Modules (SOM) and it's Interface Definition Language (IDL) that Apple has been using for it's more recent object-oriented system additions.

Dynamic Binding
Methods and classes may be added or deleted at run time. The resulting list of objects are bound together during run time using the Dynamic Typing information. This provides the flexibility to develop programs in pieces using stepwise refinement to create an initial version of a program and then later during the maintenance phases of a programs life to incrementally change the components of a program.

Dynamic Loading
Finally, program segments are not loaded into memory until they are actually used thereby minimizing the system resources required for an instance of a program. In Objective-C all of this is bound into the language and it's run time. Objective-C is possibly one of the best blends of traditional system programming language features and object-oriented programming features.

Objective-C was developed by Brad J. Cox to add object-oriented SmallTalk-80 based extensions to the C language. A GNU version was written by Dennis Gladding in 1992 and the second version soon thereafter by Richard Stallman. The current GNU version is derived from the version written by Kresten Thorup when he was a university student in Denmark in 1993. Kresten transported that version to NeXT when he joined the firm later that same year.

Objective-C Components
Objective-C is organized as a series of object-oriented additions to the C language. It is important to remember that it is the ANSI C language at its core. In a typical Objective-C program, segments of traditional C are organized into strips of code and associated data which are executed under tightly constrained conditions. This new framework for C has a well defined family of components that must be understood for the overall program to make sense. The members of the Objective-C family are: Objects Objects associate data and operations. Objects are the root of the Objective-C family tree. Methods Methods are the operations that Objective-C applies to data. Messages Messages are the way one instance of a method requests another method to perform and operation. For example [myrect display] asks the myrect method to perform the display operation. Classes Classses are how objects are defined. Classes contain the prototype object variables and methods. Classes inherit variables and methods from a higher level class called a super-class. A class that inherits some or all of the methods and variables is a sub-class. In Objective-C all classes are a sub-class of a primal super-class called Object. Protocols Protocols are ways to inherit part of a super-class or to extend a super-class. Categories Categories are ways to declare Methods that can be implemented by any class. Remote Messages Remote Messages support the distribution of objects into different threads of execution and across different computers. Remote Messages are the heart of distributed application progressing. Multi-tiered distributed applications are one of the hallmark features of the OpenStep architecture. Persistance Objects and their data can be allowed to persist after the creating instance of an object has completed it's execution. This is an important and advanced feature of the OpenStep system. In traditional programming languages a function is called to perform an operation on a data object. In Objective-C and other object-oriented languages, a message is sent to an object asking it to perform one of its methods on itself.

A Practical Analogy
Many file systems are organized into standard primitive operations such as:

Open - open or create a file

• • • •

Close - close a file Read - read data from a file Write - write data to a file Ioctl - perform file system specific operations

The Open call usually returns some sort of identifier that is used in subsequent Read, Write and Ioctl operations. The identifier is valid until a Close call is made. In an object-oriented environment, the class might be named FileSystem, the methods would be Open, Close, Read, Write and Ioctl. The object would be the file identifier that is returned by the Open call and binds all subsequent calls to a single body of file information.

Objective-C Example
This example is also take from Gerrit Huizenga's course notes from an object-oriented language course taught at Purdue University. The URL for the course notes and other reference information are contained later in the reference section.

In Objective-C program segments are defined by an interface and an implementation. These are by convention separated into two files. The interface file is named with a '.h' extension and the implementation file is named with a '.m' extension.

Interface Definition
In Objective-C the interface to a class is defined using the @interface and @end pair of language statements. @interface Stack : Object Defines the Stack class as a subclass of the Object super-class. This declaration is followed by the instance variables used to implement the object. Each instance of a class has it's own copy of these variables.
@interface Stack : Object { StackLink *top; unsigned int size; }

Defines the Stack class with instance variables top and size; Following this definition is a list of the names of the methods that implement this class.
@end free; push : (int) anInt; (int) pop; (unsigned int) size;

Defines free, push, pop and size methods for the Stack class. The declaration is terminated with an @end statement. Method names contain a colon to separate the name of a method from its arguments. A colon is also used to separate arguments. For example:

- push: (int) first and: (int) second;

Defines the push method with integer arguments first and second. The precise name of the method is push:and:. Any parameter type used in C language function parameter "typecast" notation is valid in method definitions. The return type of a method is also specified using typecast notation. If no return type is specified, then the method is assumed to return a pointer to an object. At first glance the alloc method would naturally be another method that might be included with the Stack class. We will see below that every class has a means to create an instance or allocate itself.
#import <objc/Object.h> @interface Stack : Object { Stacklink *top; unsigned int size; } - free; - push : (int) anInt; - (int) pop; - (unsigned int) size; @end

is the completed interface definition for the Stack class. It is stored, by convention in the file Stack.h.

The second component of a class definition is the implementation. Like the interface definition Objective-C uses paired sets of @implementation and @end statements to deliniate an implementation:
@implementation Stack . . . @end

Defines the Stack implementation. By convention the implementation of a class is stored in a file the ends with a '.m' suffix. In this case the file would be named Stack.m.
#import "Stack.h" #define NULL_LINK StackLink *0 @implementation Stack + new { self = [super new]; top = NULL_LINK; return( self ); } - free


StackLink *next; while( top != (StackLink *)0 ) { next = top->next; free( (char *)top ); top = next; } return( [super free] );

} - push: (int )value { StackLink *newLink; newLink = (StackLink *) malloc( sizeof(StackLink) ); if( newLink == 0 ) { fprintf( stderr,"Out of Memory\n" ); return( 0 ); } newLink->data = value; newLink->next = top; top = newLink; size++; return( self ); } - (int) pop { int value; StackLink *topLink; if( size == 0 ) value = 0; else { topLink = top; top = top->next; value = topLink->data; free( topLink ); size--; } return( value ); } - (unsigned int) size { return( size ); } @end

The implementation defines methods new, free, push, pop and size.

Super-class References
Bracketed statements such as [super new] and [ super free ] refer to the super-class methods new and free allowing them to process at particular points of the sub-class.

The Notion of Self
Self is a special variable which is a pointer to the object which received a message which invoked the currently executing method. It is a way to reference the receiver object of a message. Also remember that if a method does not specify the type of it's return value, the default is to return a pointer to an object. This means that barring any other sensible returns, methods should always return self. Doing this provides a means for super-class and sub-class implementations to share processing and return values.

Factory Objects
Objective-C automatically creates a factory object for each class. There is exactly one instance of a factory object at run time. The name of the factory object is the same as the name of the class. The principal use of a factory object is to provide a means to create instances of a class:
id myStack; myStack = [ Stack new ];

Factory methods are defined with a '+' instead of a '-' character.
+ new {

self = [super new]; top = NULL_LINK; return( self );


Factory objects are not instances of a class and therefore do not have access to the instance variables. So factory objects typically redefine self before accessing instance variables.

Referencing a Class
Objective-C is ANSI C and additional abilities to define classes, create instances of objects and send messages to objects. Two fundamental types have been added to the language: id - a pointer to an object • sel - a messages Both variables of type id and sel are valid parameters that can be sent in messages or passed to a C function.

Messages are sent to a class with a SmallTalk-like syntax:
id s; int i; s = [ Stack new ]; // send "new" message to factory object Stack [s push:97 ]; // send "push" message w/param 97 to s

i = [ s pop ]; // send "pop" message to s

In the Objective-C implementation, methods access and modify instance variables to perform their operations. Methods perform all operations normally thought of in C. In addition, they have access to the full capabilities of Objective-C message passing with the notions of self, super-class, parameters and instance variables. An object might send a message to self to accomplish parts of it's operation. Here is the stack:and: method implemented by using paired stack push operations and self:
- push: (int) first and: (int) second { [ self push: first ]; [ self push: second ]; return( self ); }

Again notice that self is returned to facilitate super-class and sub-class integration. Source: http://www.tenon.com/products/codebuilder/Objective-C.shtml

Sign up to vote on this title
UsefulNot useful