---
title: "What is protocol?"  
description: "What is protocol?"  
author: "Tarun Kumar"  
published: 2015-07-21  
updated: 2020-09-18  
canonical: https://www.mindstick.com/interview/2718/what-is-protocol  
category: "iphone"  
tags: ["iphone", "ios", "objective c"]  
reading_time: 3 minutes  

---

# What is protocol?

**A protocol** is a list of method declarations that is not bound to any one class. The methods are not implemented in the protocol; instead, classes conform to or adopt a protocol and within those classes the methods are implemented. A protocol is simply a grouping of certain related methods under one name, and it acts like a contract—conforming classes must implement the required methods (and may implement any of the potential optional methods).

Here is an example of a protocol which includes one method, notice the instance variable delegate is of type id, as it will be unknown at compile time the type of class that will adopt this protocol.

```
#import< Foundation/Foundation.h> @protocol ProcessDelegate< NSObject>@required    - (void) processSuccessful: (BOOL)success;@end @interface ClassWithProtocol : NSObject {    id <ProcessDelegate> delegate;}  @property (retain) id delegate; -(void)startSomeProcess; @end
```

**Protocol Implementation**

Inside the implementation section for the interface defined above we need to do two things at a minimum – first synthesize the delegate instance variable and second, call the method defined in the protocol as needed (more on that in a moment).

Let’s look at a bare bones implementation of the SampleWithProtocol.m:

```
#import "ClassWithProtocol.h"  @implementation ClassWithProtocol  @synthesize delegate;     - (void)processComplete {                [[self delegate] processSuccessful:YES];    }     -(void)startSomeProcess {               [NSTimer scheduledTimerWithTimeInterval:5.0 target:self                                  selector:@selector(processComplete) userInfo:nil repeats:YES];    } @end
```

## Answers

### Answer by Tarun Kumar

**A protocol** is a list of method declarations that is not bound to any one class. The methods are not implemented in the protocol; instead, classes conform to or adopt a protocol and within those classes the methods are implemented. A protocol is simply a grouping of certain related methods under one name, and it acts like a contract—conforming classes must implement the required methods (and may implement any of the potential optional methods).

Here is an example of a protocol which includes one method, notice the instance variable delegate is of type id, as it will be unknown at compile time the type of class that will adopt this protocol.

```
#import< Foundation/Foundation.h> @protocol ProcessDelegate< NSObject>@required    - (void) processSuccessful: (BOOL)success;@end @interface ClassWithProtocol : NSObject {    id <ProcessDelegate> delegate;}  @property (retain) id delegate; -(void)startSomeProcess; @end
```

**Protocol Implementation**

Inside the implementation section for the interface defined above we need to do two things at a minimum – first synthesize the delegate instance variable and second, call the method defined in the protocol as needed (more on that in a moment).

Let’s look at a bare bones implementation of the SampleWithProtocol.m:

```
#import "ClassWithProtocol.h"  @implementation ClassWithProtocol  @synthesize delegate;     - (void)processComplete {                [[self delegate] processSuccessful:YES];    }     -(void)startSomeProcess {               [NSTimer scheduledTimerWithTimeInterval:5.0 target:self                                  selector:@selector(processComplete) userInfo:nil repeats:YES];    } @end
```


---

Original Source: https://www.mindstick.com/interview/2718/what-is-protocol

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
