Open In App

Class Hierarchy in Objective-C

Last Updated : 13 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

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. 

The Objective-C class hierarchy consists of various types and subtypes of sophistication, with the root class being NSObject: 

Root Class

At the pinnacle of the hierarchy is the root class, which is named NSObject. All different lessons in Objective-C are derived from this class, and it presents the primary functionality that is inherited via all different classes. The root class is also called as the absolute parent class of all the inherited classes. 

Base Classes

Below the root elegance, are the base classes, which encompass classes such as NSNumber, NSArray, and NSString. These lessons provide the fundamental capability for common responsibilities along with storing and manipulating records.

Framework Classes

Framework classes are constructed on the pinnacle of the base instructions and provide superior capability for particular responsibilities. For example, training encompasses Core Data for database management and UI Kit for constructing consumer interfaces.

Custom Classes

All, the above classes discussed above are used by the developer, for providing precise functionality for their software, and are less used. Generally, developers work on the custom classes which can inherit residences and techniques from the base and framework instructions, in addition to different custom instructions. Here is an example of a custom class that inherits from the NSObject. 

ObjectiveC




// Creating a class name, MyObject, 
// which is inherited from the root class
// i.e. NSObject. 
@interface MyObject : NSObject {
      // adding a variable to the class MyObject. 
    int myInt;
}
  
// Declaring functions of MyObject class 
- (void)getMyInt;
- (void)setMyInt:(int)value;
  
@end
  
// Here, comes the implementation
// of the blue print created above. 
@implementation MyObject
  
// Implementing getMyInt function. 
- (void)getMyInt {
    NSLog (@"%ld", myInt);
}
  
// Implementing setMyInt function. 
- (void)setMyInt:(int)value {
    myInt = value;
}
  
@end


Syntax, and Keywords

Many key phrases are used which is used to define the inheritance of the class. Some of the most crucial key phrases are shown below:

  • @interface: Defines the interface for a category, such as its homes and techniques.
  • @implementation: Implements the techniques described in the interface.
  • @belongings: Defines belongings for a class, including its records type, getting the right of entry to manage, and other attributes.
  • @synthesize: Generates getter and setter techniques for belonging.
  • @protocol: Defines a protocol, which is a difficult and rapid technique that a class can put into effect to comply with a particular behavior.

Inheritance Overriding 

We come up with many real-life situations when a child class has only some features, different from the parent class, or may also have some additional features. Now, we have two choices, either we copy the entire function in the child class and then amend the required changes, or override the function of the parent class, later is a better choice. Overriding means redefining, the function, for that particular environment or class. The overriding, function must take the same number of arguments as the parent class, also with the same return type. For example, in general. child’s physical appearance matches with their parents but with some additional or modified features. The below code shows the inheritance overriding in the MyObject1 class. MyObject1 class is a child class of MyObject class. In MyObject class, getMyInt(), prints only the myInt variable, and in MyObject1 class, getMyInt(), prints myInt, and myInt2 variables.

ObjectiveC




// Creating a class name, MyObject1, 
// which is inherited from the another sub-class
// i.e. MyObject. 
@interface MyObject1 : MyObject {
      // adding a variable to the class MyObject1. 
    int myInt1;
}
  
// Declaring functions of MyObject1 class 
- (void)getMyInt;
- (void)setMyInt1:(int)value;
  
@end
  
// Here, comes the implementation
// of the blue print created above. 
@implementation MyObject
  
// Inheritance overriding getMyInt function. 
- (void)getMyInt {
    NSLog (@"%ld %ld", myInt, myInt1);
}
  
// Implementing setMyInt1 function. 
- (void)setMyInt1:(int)value {
    myInt1 = value;
}
  
@end


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.geeksforgeeks.org/difference-between-c-and-objective-c-2/. 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads