Class Hierarchy in Objective-C
Last Updated :
24 Apr, 2025
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
@interface MyObject : NSObject {
int myInt;
}
- ( void )getMyInt;
- ( void )setMyInt:( int )value;
@end
@implementation MyObject
- ( void )getMyInt {
NSLog ( @"%ld" , myInt);
}
- ( 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
@interface MyObject1 : MyObject {
int myInt1;
}
- ( void )getMyInt;
- ( void )setMyInt1:( int )value;
@end
@implementation MyObject
- ( void )getMyInt {
NSLog ( @"%ld %ld" , myInt, myInt1);
}
- ( 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
NSArray *myArray = [[ NSArray alloc] initWithObjects: @"Object 1" , @"Object 2" , @"Object 3" , nil ];
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
@protocol MyProtocol
- ( void )doSomething;
@end
@interface MyClass : NSObject <MyProtocol>
@end
@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/.
Similar Reads
Classes & Objects in Objective-C
Objective-C is an object-oriented programming language that has been used for developing software applications for various Apple platforms, such as iOS, macOS, watchOS, and tvOS. Classes and objects are the fundamental building blocks of object-oriented programming in Objective-C. A class is a bluep
8 min read
Categories in Objective-C
Categories are an important concept in the Objective-C programming language. They allow developers to extend the functionality of existing classes without having to modify their original code. This article will discuss categories in Objective-C, their uses, and provide examples of their implementati
3 min read
Interface in Objective-C
Objective-C is an object-oriented programming language that was developed by Apple Inc. for its operating systems. One of the fundamental concepts of object-oriented programming is the ability to define interfaces, which specify the methods and properties that an object must have in order to be cons
7 min read
Singleton Class in Objective-C
Objective-C serves as a programming language widely employed to construct applications intended for both macOS and iOS. Among the many design patterns that find extensive usage in Objective-C, the Singleton pattern proves quite noteworthy. A Singleton represents a class in that instantiation remains
6 min read
Constructors in Objective-C
Constructor is basically a function or method that is called automatically at the time of object creation of a class. constructor method has the same name as that of the class. It is basically used to initialize the data members of the new objects generally. It constructs or creates a new value for
8 min read
Blocks in Objective-C
Blocks in Objective-C are a potent tool that can simplify the development process. Blocks are an alternative to traditional functions, allowing you to write code that is more concise and efficient. They are used for many different purposes, including as callbacks and iterators. Blocks can also be us
6 min read
Type Casting in Objective-C
Objective-C is a programming language that was created in the 1980s and is widely used for developing software for the macOS and iOS platforms. One of Objective-C's key features is its ability to perform typecasting. Type casting enables programmers to convert one data type to another, which is usef
4 min read
Arrays in Objective-C
An Array is a data structure that holds similar types of data in contiguous memory. The Objective-C array is a single variable array that is used to store different types of elements in the array. Also, we can access the objective-c array using an index. It is quite similar to the C programming lang
5 min read
Constants in Objective-C
Constants in Objective-C are values that cannot be modified once they are set. They are used to store a variety of data types, including numbers, strings, and booleans. Constants are a fundamental feature of the Objective-C programming language and are widely used in the development of applications
4 min read
File Handling in Objective-C
File handling is an essential part of any programming language, and Objective C is no exception. It allows developers to create, read, update, and delete files on the file system. This can be useful for a wide range of applications such as storing user data, saving game progress, or logging applicat
9 min read