Additional Concepts and Examples

We have understood the syntax of the inheritance class in objective C. The below examples show how to create an NSArray object, and create custom protocols. 

Creating an NSArray object

The below example shows the syntax of creating an NSArray object in objective C. This creates an NSArray object with the use of the initWithObjects technique and gives 3 string objects to it.

ObjectiveC




// Creating an object of NSArray
NSArray *myArray = [[NSArray alloc] initWithObjects:@"Object 1", @"Object 2", @"Object 3", nil];
  
// Displaying the myArray
NSLog(@"%@", myArray);


Example 2: Creating a custom protocol

This code defines a custom protocol named MyProtocol that consists of a method called doSomething. It then defines a custom class named MyClass that implements the MyProtocol protocol and includes an implementation of the doSomething method. Finally, the doSomething method is referred to as the usage of NSLog

ObjectiveC




// Creating blue print of the protocol
@protocol MyProtocol
  
// function in the protocol
- (void)doSomething;
  
@end
  
// Inherited MyClass from NSObject, with 
// the MyProtocol
@interface MyClass : NSObject <MyProtocol>
  
@end
  
// Implementing the MyClass class. 
@implementation MyClass
  
- (void)doSomething {
    NSLog(@"Doing something...");
}
  
@end


Difference between C++ and Objective-C Class Inheritance

C++ has the capability of multiple inheritances, but objective-C does not provide the functionality of multiple inheritances. To know more about it, refer to the article https://www.w3wiki.org/difference-between-c-and-objective-c-2/. 



Class Hierarchy in Objective-C

Objective-C is an effective programming language this is broadly used for growing applications on Apple’s macOS and iOS platforms. Objective-C is an object-oriented programming language, hence, providing the capability of the class hierarchy. Class hierarchy means acquiring features of the base class, with its own customizable features, and also some add-on features. Hierarchy is important for the real-world designing of applications. It permits developers to create complicated object-oriented packages by defining and organizing them in a hierarchical manner. In this article, we will learn class hierarchy in objective C. 

Similar Reads

Class Hierarchy

Objective-C, classes can inherit homes and techniques from their instructions, taking into consideration code reuse and simplifying software improvement. Understanding the elegance hierarchy in Objective-C is crucial for developing green and scalable applications....

Additional Concepts and Examples

...

Contact Us