Protocol and protocol inheritance in swift

Nitish Kumar
3 min readJan 10, 2022

In this article we will see what is protocol and protocol inheritance?

Lets start, First of all,

What is protocol?

A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality.

Lets create one simple example:

For that create we will protocol as shown below.

protocol Audi {//properties of Audi//{get set} is used to declare gettable and settable properties after their type declaration.var model : String { get set }var color : String { get set }}

Now, we can conform struct AudiModel to protocol Audi as shown below.

struct AudiModel : Audi {var model: Stringvar color: String}let AudiModel1 = AudiModel(model: “R3”, color: “white”)

Okay that was easy, Now we will try to create little complicated protocol.

protocol Audi {var model : String { get set }var color : String { get set }var agencyMangerName : String { get set }var totalEmployee : Int { get set }}
struct AudiModel : Audi {var model: Stringvar color: Stringvar agencyMangerName: Stringvar totalEmployee: Int}
struct AudiShowroom : Audi {var model: Stringvar color: Stringvar agencyMangerName: Stringvar totalEmployee: Int}

As you can see for my struct audimodel, i just want two properties i.e. model and car but i am getting all 4 properties which are not required for struct AudiModel. 😒😒

Here we are violating SOLID principle “I” i.e. “Interface segregation principle” ,which say that a client should not be exposed to methods it doesn’t need.

What to do now? 😒😒

Now, we can use protocol inheritance where protocol can inherit other protocol and make your life easier 😇.

1. Create a Audi protocol

protocol Audi {var model : String { get set }var color : String { get set }}

2. Create a AudiShowroom protocol

protocol AudiShowroom {var agencyMangerName : String { get set }var totalEmployee : Int { get set }}

3. Create a Audi_Car_Showroom protocol and inherit Audi and AudiShowroom protocol

protocol Audi_Car_Showroom : Audi,AudiShowroom {}

4. Create a struct AudiShowroomDetail which conform AudiShowroom protocol

struct AudiShowroomDetail :AudiShowroom{var agencyMangerName: Stringvar totalEmployee: Int}

5. Create a struct which conform Audi_Car_Showroom protocol

struct Audi_Car_Showroom_Detail : Audi_Car_Showroom {var model: Stringvar color: Stringvar agencyMangerName: Stringvar totalEmployee: Int}let AudiShowroomDetail1 = AudiShowroomDetail(agencyMangerName: “Jhon”, totalEmployee: 80)let Audi_Car_Showroom_Detail1 = Audi_Car_Showroom_Detail(model: “X8”, color: “Red”, agencyMangerName: “David W”, totalEmployee: 100)

In this way you can create seperate protocols and use it as needed.

BONUS :

How to use function in protocol.

//protocol is for defining the function.protocol Audi {}//extension is for implementing the function.extension Audi {func launchDate(){     }}struct AudiFactory : Audi{func launchDate(){print(“relesed from factory”)      }}struct AudiMarket : Audi {func launchDate(){print(“not relesed in market”)     }}AudiFactory().launchDate()AudiMarket().launchDate()

Output:-

relesed from factorynot relesed in market

Hope you enjoyed reading this post!!

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Thank you!!! Happy coding.😇😇

--

--

Nitish Kumar

I developed and maintain applications aimed at a range of iOS devices including mobile phones and tablet computers.