EZ

Eduzan

Learning Hub

Eduzan
Eduzan / Objective C

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.
End of lesson.