# Cocoa Programming/Objective-C basics

* Cocoa Programming First Steps | Objective-C basics | Memory Management | Foundation Kit | Application Kit | Web Kit
Objective-C is the native language of Cocoa applications. It is an object-oriented superset of ANSI C, allowing the developer to include "plain C" statements and code into the program. Any ANSI C program is a valid Objective-C program.

## NSObject

The Objective-C used in Cocoa is a bit different from "plain" Objective-C, because of the Cocoa runtime system which defines a new root class, NSObject. All Cocoa objects should be descendants of NSObject in order to gain from its improved memory management model.
NSObject is defined in the header <Foundation/NSObject.h>.

### The NSObject life cycle

To create an NSObject, send its class either an alloc or a new message:
Although "new" and "alloc followed by init" methods appear to be identical, Apple documentation seems to give preference to the latter method. This gives developers the ability to override alloc or init separately to achieve a certain result, if necessary.
Note that initializing an object may sometimes fail: for example a file containing the initialization data may be unavailable. In that case, init will return nil. Therefore, it is always necessary to check the return value of init before using the object!
After an object has been initialized, it is ready for use. To use an object, send it messages:
If the object understands the message, it will react to it. Otherwise, a trap is generated.
When an object is no longer used, it must be released. To release an object, send it a release message.
This will lead to the destruction of the object (unless it's still in use somewhere else, see Cocoa memory management for details).
When an object is about to be destroyed, it will receive a dealloc message. If the object has reserved any resources, it should release them now.

## Extending NSObject

All classes in an Cocoa/Objective-C application should be derivatives of NSObject. Thus, when defining a new class you should follow some basic rules:
* Derive from NSObject:
* Either directly or from a derivative
* Define initializer & deallocator
* Call base class initializer & deallocator

### Deriving from NSObject

To create your class as a subclass of NSObject, define it as follows (in the interface definition file "MyClass.h"):

### Defining initializer and deallocator

An initializer is a method that initializes an object. In Cocoa, the initializer for NSObject is called init. The init method for NSObject does not know about user-defined classes and cannot therefore initialize their instances properly. Thus, it is essential to write an initializer method for your class. If the initialization does not require any parameters, the method to define is called init (i.e., you should override the base class initializer). This makes it possible to create instances of your class by using the new class method derived from NSObject:
The above code creates a new instance of MyClass. Since MyClass has no methods called alloc or init, the messages are automatically sent to its base class.
To override the initializer for your class, optionally define it in the interface file:
and implement it in the.m file:
If the initialization requires additional parameters, you should define an initializer like initWithParam:, for example
Even then, it is often advisable to define a "default" initializer that calls your initWithXXX method with suitable default parameters:
A deallocator is a method that is responsible to release all resources an object holds. The deallocator is called before an object is released, thus making sure everything gets cleaned up properly. In Cocoa, the deallocator method is dealloc. Every class that holds any resources should define a dealloc method. For example:

### Calling the base class initializer and deallocator

When a class is derived from some other class (as the MyClass was derived from NSObject), the instances created from it are not only instances of the derived class, but also instances of the base class. This is called the is-a relation. A derived class is an extension of the base class: it can do more than the base class, but it can do everything the base class can. Therefore, objects of a derived class always contain everything that objects of the base class contain. If an NSObject contains a retain count member variable, then also MyClass objects will contain it. Thus, the "base class part" of a derived object needs to be initialized and deallocated when initializing and deallocating the derived object.
To initialize the base class part of an object write
to the very beginning of the initializer of your class. To deallocate the base class part of the object write
to the very end of the deallocator of your class.

## More information

For more information on Objective-C in Cocoa, see
* Objective-C Programming for general information on Objective-C
* Objective-C GNUstep Base Programming Manual for specific information on Objective-C in Cocoa/GNUstep.