Objective-C Classes and Objects

Classes are the main feature of Objective-C, supports object-oriented programming and are sometime called as user-defined data types. A class is used to determine the form of an object and it combines the data representation and methods for manipulating that data into one neat package.

Here are some characteristics given of classes and objects in Objective-C:

  • In Objective-C, class is defined in two sections named @interface and @implementation
  • Classes hides the implementation of an object
  • Almost everything is in the form of objects
  • Objects contains instance variables
  • Objects and instance variables have scope
  • Properties are used to provide access to the class instance variables in other classes
  • Objects receive messages and objects are often referred as receivers

Class Definitions in Objective-C

Whenever you define a class in Objective-C, it means that you are defining the blueprint for a data type. A class definition starts with the keyword named @interface followed by the interface(class) name; and the class body, enclose by a pair of curly braces. All the classes are derived from the base class that is NSObject in Objective-C. This is the superclass of all the classes in Objective-C.

Here is an example, defining the Box data type using the keyword class:

@interface Box:NSObject
    /* these are instance variables */
    double len;   // Length of Box
    double bre;   // Breadth of Box
@property(nonatomic, readwrite) double hei;

The instance variables are private and are only accessible inside the class

Allocating and Initializing Objects in Objective-C

As you know that a class provides the blueprint for the objects, so an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare the variables of basic data types in Objective-C. Here, these statements declares the two objects of class Box

Box box1 = [[Box alloc]init];
Box box2 = [[Box alloc]init];

Here, both the objects that is box1 and box2 will have their own copy of the data members.

Accessing Data Members in Objective-C

The properties of objects of a class in Objective-C, can be access simply by using the direct member access operator (also called as dot (.) operator).

Objective-C Classes and Objects Example

Here is an example program illustrates the concept of classes and objects in Objective-C practically

/* Objective-C Classes and Objects - Example Program */
#import <Foundation/Foundation.h>
@interface Box:NSObject
	double len;   // Length of a box
	double bre;  // Breadth of a box
	double hei;   // Height of a box
@property(nonatomic, readwrite) double hei;
-(double) volume;

@implementation Box
@synthesize hei; 
	self = [super init];
	len = 1.0;
	bre = 1.0;
	return self;
-(double) volume
	return len*bre*hei;

int main()
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];    
	Box *box1 = [[Box alloc]init];
	Box *box2 = [[Box alloc]init];
	double vol = 0.0; 
	box1.hei = 5.0; 
	box2.hei = 10.0;
	vol = [box1 vol];
	NSLog(@"Volume of box1 = %f", vol);
	vol = [box2 vol];
	NSLog(@"Volume of box2 = %f", vol);
	[pool drain];
	return 0;

When the above code is compile and executed, it will produce the following result:

2010-08-03 13:18:37.932 ClassAndObjects[387:303] Volume of box1 = 5.000000
2010-08-03 13:18:37.932 ClassAndObjects[387:303] Volume of box2 = 10.000000

Objective-C Online Test

« Previous Tutorial Next Tutorial »