Inheritance in Objective-C
Last Updated :
24 Apr, 2025
In general, Inheritance is a mechanism by which a subordinate or child class acquires the traits and qualities of a superordinate class or other derived classes. It also allows extra features like taking child class properties and using them in other derived classes. The syntax of classes is used to implement inheritance in Objective-C. A class also referred to as a superclass or parent class, can inherit the attributes and functions of other classes. The term "subclass" or "child class" refers to the class that inherits these attributes and methods.
The syntax for defining Subclass:
@interface SubclassName : SuperclassName //Name of the super class
// additional methods of the subclass
@end
A subclass inherits all of the instance variables, attributes, and methods of the superclass when it is defined. The subclass can also provide its own implementation of a method by overriding one from the superclass.
Sample Program:
ObjectiveC
#import <Foundation/Foundation.h>
@interface Parentclass : NSObject {
NSString *Name;
NSInteger Age;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end
@implementation Parentclass
- (id)initWithName:(NSString *)name andAge:(NSInteger)age {
Name = name;
Age = age;
return self;
}
- (void)print {
NSLog(@"Name: %@", Name);
NSLog(@"Age: %ld", Age);
}
@end
@interface Childclass : Parentclass {
NSString *Dept;
}
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
andDept:(NSString *)department;
- (void)print;
@end
@implementation Childclass
- (id)initWithName:(NSString *)name andAge:(NSInteger)age
andDept: (NSString *)department {
Name = name;
Age = age;
Dept = department;
return self;
}
- (void)print {
NSLog(@"Name: %@", Name);
NSLog(@"Age: %ld", Age);
NSLog(@"Department: %@", Dept);
}
@end
int main(int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Parent class");
Parentclass *parentclass = [[Parentclass alloc]initWithName:@"Sai" andAge:19];
[parentclass print];
NSLog(@"Parent Class is Inherited");
Childclass *childclass = [[Childclass alloc]initWithName:@"Sai"
andAge:19 andDept:@"CSE"];
[childclass print];
[pool drain];
return 0;
}
Output:
Types of Inheritances
In Objective-c there are two types of inheritances. They are:
- Single Inheritance
- Multiple Inheritance
Single inheritance
In Objective-C, single inheritance is the type that is most frequently employed. A subclass inherits the methods and attributes of a single superclass under single inheritance. The subclass has the ability to replace any of the methods from the superclass as well as add new attributes and methods of its own. Because multiple inheritance is not supported by Objective-C, inheritance hierarchies can only be implemented in Objective-C using single inheritance.
Example Program:
ObjectiveC
// Animal.h
@interface Animal : NSObject
@property NSString *name;
-(void)makeSound;
@end
// Animal.m
@implementation Animal
-(void)makeSound {
NSLog(@"The animal makes a sound");
}
@end
// Dog.h
@interface Cat : Animal
@end
// Dog.m
@implementation Cat
-(void)makeSound {
NSLog(@"The cat sounds");
}
@end
// main.m
#import <Foundation/Foundation.h>
#import "Cat.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Cat *myCat = [[Cat alloc] init];
myCat.name = @"Tony";
[myCat makeSound];
}
return 0;
}
Output:
Multiple Inheritance
Objective C does not directly enable multiple inheritances. However, it is possible because of a notion known as protocols. A class can implement a protocol's set of methods. A class can inherit the methods specified in a protocol by implementing the protocol. Since a class can implement numerous protocols, it can inherit methods from various sources.
Example Program:
ObjectiveC
// Swimming.h
@protocol Swimming
-(void)swim;
@end
// Running.h
@protocol Running
-(void)run;
@end
// Animal.h
#import "Swimming.h"
#import "Running.h"
@interface Animal : NSObject <Swimming, Running>
@property NSString *name;
@end
// Animal.m
@implementation Animal
-(void)swim {
NSLog(@"%@ is swimming", self.name);
}
-(void)run {
NSLog(@"%@ is running", self.name);
}
@end
// main.m
#import <Foundation/Foundation.h>
#import "Animal.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Animal *myAnimal = [[Animal alloc] init];
myAnimal.name = @"Dog";
[myAnimal swim];
[myAnimal run];
}
return 0;
}
Output:
The class "Animal" is next, and it adopts both the "swimming" and "running" procedures. We define a property name and put the methods from the "Swimming" and "Running" protocols into the "Animal" protocol. We create a new instance of the "Animal" class in the main.m, change its name property to "Dog," and then call the "swim" and "run" methods. "Dog is swimming", followed by "Dog is running," since "Animal" accepts both the "Swimming" and "Running" procedures.
Similar Reads
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
Numbers in Objective-C
Normally we work with numbers in almost every programming language. But in all other programming languages when we work with numbers we use primitive data types like int, short, long, float, etc. in Objective - C programming numbers have a very wide range we store the basic data types always in the
6 min read
C++ Multilevel Inheritance
Multilevel Inheritance in C++ is the process of deriving a class from another derived class. When one class inherits another class it is further inherited by another class. It is known as multi-level inheritance. For example, if we take Grandfather as a base class then Father is the derived class th
2 min read
Functions in Objective-C
Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It is the main programming language used by Apple for the OS X and iOS operating systems and their respective application programming interfaces (APIs), Cocoa and
8 min read
Overriding Inherited Methods in Objective-C
Objective-C is an object-oriented programming language that is widely used in the development of iOS and Mac applications. Inheritance is an important concept in object-oriented programming that allows classes to inherit properties and behavior from other classes. In Objective-C, inheritance is the
9 min read
Extensions in Objective-C
In contrast to an implementation described in a category, Extensions (Class Extensions) are a specific kind of category that necessitates that its methods be declared in the main implementation block for the associated class. Publicly declared property attributes may be overridden with this. Similar
3 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
Memory Management in Objective-C
Memory management is a core concept irrespective of any programming language. The process of allocation of memory of objects when needed and deallocation when they are no longer in use is called Memory Management. Memory is a limited resource and so has to be dealt with carefully. If unrequired memo
7 min read
Hybrid Inheritance In C++
Before jumping into Hybrid Inheritance, let us first know what is inheritance. Inheritance is a fundamental OOP concept in C++ that allows a new class, also known as a subclass or derived class, to inherit properties and methods from an already-existing class, also known as a superclass or base clas
6 min read
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 clas
5 min read