1_Objective-C Classes & Objects

What is the class

  • Classes are the central feature of Objective-C
  • Often class called user-defined types.

Whey we use the class

  • Defined new types
  • A class is used to specify the form of an object
  • class combines data representation and methods for manipulating that data into one neat package.
  • The data and methods within a class are called members of the class.

How to defined class

The class is defined in two different sections namely

  • @interface
  •  @implementation.

Objective characteristics

  • Almost everything is in form of objects.
  • Objects receive messages and objects are often referred as receivers.
  • Objects contain instance variables.
  • Objects and instance variables have scope
  • Classes hide an object’s implementation.
  • Properties are used to provide access to class instance variables in other classes.

Objective-C Class Definitions:

  • A class definition starts with the keyword @interface followed by the interface(class) name;
  • class body, enclosed by a pair of curly braces.

Note :  In Objective-C, all classes are derived from the base class called NSObject.


The class is defined in two different sections namely @interface and @implementation.

Section of interface

@interface interface_name:NSObject 
     //Instance variables
     data_type variable_name;
 // Property
@property(access_specifiers,access_specifiers) data_type variavble;
-(data_type) function_name; //shows how to declare a method.
@end //marks the end of an interface.


@property(access_specifiers,access_specifiers) data_type variavble;

Properties are introduced in Objective-C to ensure that the instance variable of the class can be accessed outside the class.

The various parts are the property declaration are as follows.

  • Properties begin with @property, which is a keyword
  • It is followed with access specifiers
  • Finally, we have the property name terminated by a semicolon.

Access specifiers

  • nonatomic or atomic
  • readwrite or readonly
  • strong, unsafe_unretained or weak.

This varies based on the type of the variable.

Note : The instance variables are private and are only accessible inside the class implementation.

Section of implementation

@implementation interface_name //shows how to implement the interface.
@synthesize variables_name; //Use with Instance variables with properties
-(data_type) function_name// the same in interface
 return values;
@end //the end of an implementation.

Synthesize statement

Implements property setters and getters

But in the latest XCode, the synthesis part is taken care by the XCode and you need not include synthesize statement.

Allocating and initializing Objective-C Objects:


interface_name *object_name1 = [[interface_name alloc]init]; // Create object_name
interface_name *object_name2 = [[interface_name alloc]init]; // Create object_name

Note : Both of the objects name1 and name2 will have their own copy of data members.

Accessing the Data Members:

The properties of objects of a class can be accessed using the direct member access operator (.)

Full example about creating objects and class

“id” data type

“id” is a data type of object identifiers in Objective-C, which can be use for an object of any type no matter what class does it have. “id” is the final supertype of all objects. In the simplest form this statement:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s