Open In App

Inheritance in Objective-C

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

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:

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:

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:

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads