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


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