Header Ads

Classes and Objects - Objective C

The main purpose of Objective C programming language is to add object orientation to the C programming language and classes are the central feature of objetive C that support object oriented programming language and are often called user-defined types.

A class is used to specify the form of and it combines data representation and methods for manipulation that the data into one neat package. The data and methods within a class are called members of the class.

Objective C characteristics
  • The class is defined in two different section namely @interface and @implementaion
  • Almost everything is in form of objects.
  • Objects receive messages and objects are often referred as receivers.
  • Objects contain instance variables.
  • Objects and instance variable 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

When you define a class , you define a blueprint for a data type. The doesn't actually define any data, but it does define what the class name means, that is what an object of the classes will consist of and what operations can be performed on such an object.

A class definition starts with the keyword @interface followed by the interface (class) name and the class body, enclosed by a pair of curly braces. In objective C all classes are derived from the base class called NSObject. It is the superclass of all Objecttive C classes. It provides basic methods like memory allocation and initialization. For example, we defined the Box data type using the keyword class as follows:

@interface Box : NSObject
{
      double length;
      double breadth;
}
@property (nonatomic, readwrite) double height;

@end


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

Allocation and initializing Objective C objects

A class provides the blueprints for objects, so basically an object is created from a class. We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box.

Box box1 = [[Box alloc]init];        //create box1 object of type Box
Box box2 = [[Box alloc]init];

Both of the objects box1 and box2 will have their own copy of data members.

Accessing the Date Members

The properties of objects of class can be accessed using the direct member access operator (.) . Let is try the following example to make thing clear:

#import <Foundation/Foundatio.h>
@interface Box : NSObject
{
      double length;
      double breadth;
      double height;
}
@property (nonatomic, readwrite) double height;

- (double) volume;

@end

@implementation Box
       
          @synthesize height ;
 -(id)init
{
    self =  [super init];
     length = 1.0;
     breadth = 1.0;
    return self ;
}
- (double) volume
{
      return length * breadth * height ;
}
@end

int main()
{
   Box *box1 = [[Box alloc] init];
   Box *box2 = [[Box alloc] init];

   double volume = 0.0;
  box1.length = 5.0;
  box2.height = 10.0;

  volume = [box1 volume];
  NSLog(@"volume of box1 : %f\n", volume);

  volume = [box2 volume];
  NSLog(@"volume of box2 : %f\n", volume);

  return 0;
}

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

The variable parts are the property declaration are as follows:
  • Properties begin with @property which is a keyword.
  •  It is followed with access specifiers, which are nonatomic or atomic, readwrite or readonly and strong ,unsafe_unretained or weak. This varies based on the type of the variable. For any pointer type, we use strong, unsafe_unretined or weak. Similarly for other types we can use readwrite or readonly.
  • This is followed by the datatype of the variable.
  • Finally we have the property name terminated by a semicolon.
  • We can add synthesize statement in the implementation class. But in the latest XCode, the synthesis part is taken care by the XCode and you need not include synthesis  statement.
It is only possible with the properties we can access the instance variables of the class. Actually internally getter and setter methods are created for the properties.

For example, let's assume we have a property  @property (nonatomic, readonly) BOOL isDone. Under the hood, there are setters and getters created as shown below.

- (void)setIsDone (BOOL)isDone;
-(BOOL)isDOne;



No comments:

Powered by Blogger.