Classes & Object in Objective – C

Objective-C is an object-oriented programming language widely used for developing applications for Apple platforms such as iOS, macOS, watchOS, and tvOS. In Objective-C, classes and objects form the core of object-oriented programming. A class serves as a blueprint that defines the properties and behaviors of objects, while an object is an instance of a class.

Objective-C supports several types of classes, including abstract classes, concrete classes, and root classes. Additionally, it offers various types of objects such as mutable objects, immutable objects, and singleton objects. Below, we explore these concepts in detail with syntax, keywords, and examples.

Syntax and Keywords for Classes and Objects

Syntax and Keywords for Classes and Objects

The syntax for declaring a class in Objective-C is as follows:

@interface ClassName : SuperClassName
{
    // Instance variables declaration
}

// Properties declaration
// Methods declaration

@end
  • ClassName: The name of the class.
  • SuperClassName: The name of the class from which ClassName inherits. If no superclass is specified, NSObject is the default.
  • Instance variables are declared within {}.
  • Properties and methods follow the instance variable declaration.
Class Implementation

The @implementation keyword is used to provide the implementation of the methods declared in the @interface section:

@implementation ClassName

// Methods implementation

@end
Accessing Properties and Methods

Once an object is created, its properties and methods can be accessed using dot notation:

NSString *myCarMake = myCar.make;
Keywords and Symbols in Objective-C
  • @interface: Indicates the beginning of a class declaration.
  • ClassName: The name of the class is declared.
  • SuperclassName: The name of the class that the class being declared inherits from. If the class doesn’t inherit from any class, NSObject is used as the default superclass.
  • @property: Declares a property of the class. The attributes and type parts are optional and specify the attributes and type of the property.
  • type: The type of the property.
  • propertyName: The name of the property.
  • –: Indicates an instance method.
  • +: Indicates a class method.
  • returnType: The return type of the method.
  • methodName: The name of the method.
  • parameterType: The type of the method parameter.
  • parameterName: The name of the method parameter.

Types of Classes

Abstract Class

An abstract class serves as a base class that cannot be instantiated. It provides common methods and properties for its subclasses.

Example:

@interface AbstractClass : NSObject
- (void)method1;
- (void)method2;
@end

@implementation AbstractClass
- (void)method1 {
    NSLog(@"Method 1");
}
- (void)method2 {
    NSLog(@"Method 2");
}
@end

Output:

Method 1
Method 2
Concrete Class

A concrete class defines specific methods and properties that can be instantiated.

Example:

@interface ConcreteClass : NSObject
@property (nonatomic, strong) NSString *property1;
@property (nonatomic, assign) int property2;
- (void)method1;
- (void)method2;
@end

@implementation ConcreteClass
@synthesize property1;
@synthesize property2;
- (void)method1 {
    NSLog(@"Method 1");
}
- (void)method2 {
    NSLog(@"Method 2");
}
@end

Output:

Property1: Test String
Property2: 42
Method 1
Method 2
Root Class

The root class in Objective-C is NSObject, which provides basic methods and properties common to all classes.

#import <Foundation/Foundation.h>

@interface MyObject : NSObject
@property (nonatomic, strong) NSString *name;
- (instancetype)initWithName:(NSString *)name;
@end

@implementation MyObject
- (instancetype)initWithName:(NSString *)name {
    self = [super init];
    if (self) {
        self.name = name;
    }
    return self;
}
@end

Output:

Name: Test Name

Types of Objects

Mutable Object

A mutable object can be modified after creation. These are typically created using the NSMutable prefix.

Example:

NSMutableString *mutableString = [NSMutableString stringWithString:@"Hello"];
[mutableString appendString:@" World!"];
NSLog(@"%@", mutableString);

Output:

Hello World!
Immutable Object

An immutable object cannot be modified after creation. These are typically created using the NS prefix.

Example:

NSString *immutableString = @"Hello World!";
NSLog(@"%@", immutableString);

Output:

Hello World!
Singleton Object

A singleton ensures a single instance of a class is created during the application’s lifecycle.

Example:

@interface MySingleton : NSObject
+ (instancetype)sharedInstance;
@end

@implementation MySingleton
static MySingleton *sharedInstance = nil;

+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}
@end

Example 1: Calculating Box Volume

#import <Foundation/Foundation.h>

@interface Box : NSObject {
    double len;
    double br;
    double h;
}
@property (nonatomic, readwrite) double h;
- (double)vol;
@end

@implementation Box
@synthesize h;

- (id)init {
    self = [super init];
    len = 4.0;
    br = 6.0;
    return self;
}

- (double)vol {
    return len * br * h;
}
@end

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

    box1.h = 5.0;
    box2.h = 10.0;

    NSLog(@"Volume of Box1: %f", [box1 vol]);
    NSLog(@"Volume of Box2: %f", [box2 vol]);

    [pool drain];
    return 0;
}

Output:

Volume of Box1: 120.000000
Volume of Box2: 240.000000

Example 2: Creating a Person Object

#import <Foundation/Foundation.h>

@interface Person : NSObject {
    NSString *name;
    int age;
}
- (void)setName:(NSString *)newName;
- (NSString *)name;
- (void)setAge:(int)newAge;
- (int)age;
@end

@implementation Person
- (void)setName:(NSString *)newName {
    name = newName;
}
- (NSString *)name {
    return name;
}
- (void)setAge:(int)newAge {
    age = newAge;
}
- (int)age {
    return age;
}
@end

int main() {
    Person *person = [[Person alloc] init];
    [person setName:@"John"];
    [person setAge:30];

    NSLog(@"Name: %@, Age: %d", [person name], [person age]);
    [person release];

    return 0;
}

Output:

Name: John, Age: 30

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *