What is Protocol-Oriented-Programming (POP) in Swift with examples

Lakshmi K
4 min readMar 28, 2022

--

Protocol-Oriented Programming in Swift

In this article we will learn what POP , how to declare and use it , and why do we need it. So let’s go head…

First we will check why Do we need POP as we have traditional OOPS (Object Oriented programming) language. What is the issue raised with traditional approach? Let’s see an example then we will understand what is the problem

Example : We have 2 classes of different soap verities like below

class Soap1 { // Soap 1 which will whiten skin

func skinWhitening() {

print(“Whitens the skin”)

}

}

class Soap2 { // Soap 2 which will remove dirt , oils , etc

func removesDirt() {

print(“It will remove dirt, sweat sebum, and oils from the skin”)

}

}

Now , I want to create another Soap which has Soap1 and Soap2 properties along with new properties , So I have written code like below

class Soap3 : Soap1 { // #1 — We can’t inherit from multiple classes like both Soap1 & Soap2

func removesAcne() {

print(“removes acne”)

}

}

var mySoap = Soap3()

mySoap.removesAcne()

#1 — Problem in this line is I want to inherit soap1 and soap2 properties into Soap3 , but it will give us compile time error saying multiple inheritance is not possible , like below

So problem with classes is we can’t do multiple inheritance, at this point POP comes into picture , where it makes possible to inherit from multiple protocols.

Before we see example of Protocols with multiple inheritance , let’s know few features of protocol , this is completely for freshers who are learning what are protocols

  1. Protocols are abstract oriented , which means we will declare methods without any body. It means the class which inherited will be the responsible to implement these methods.
  2. For variable we need to mentions setter and getter properties , then only we can use them out of the protocol

Now lets see example with protocols where multiple inheritance is possible

protocol P1 { // #1

func printSum() // abstract method

}

protocol P2 {

func printMinus() // #2

}

class Calculation: P1 , P2 { // #3

var a: Int = 0 // #4

var b: Int = 0

func printSum() { // #5

print(a+b)

}

func printMinus() {

if a > b {

print(a — b)

}

else {

print(b — a)

}

}

}

var myCalculation = Calculation()

myCalculation.a = 5 // #6

myCalculation.b = 12

myCalculation.printSum() // #7

myCalculation.printMinus()

#1 — Declaring the protocol P1 by using keyword protocol

#2 — Declaring the abstract method printMinus() without any body

#3 — Here we are declaring class name Calculation inheriting from two protocols P1 and P2 // Multiple inheritance

#4 — declaring some variables for calculations

#5 — now we are implementing the P1 and P2 methods in this inherited class, we must implement or else it will give compile time errors.

#6 — We assigned some values to class variables

#7 — We are calling protocol methods from inherited class variable.

With the above example we implemented multiple inheritance by using Protocols , this entire process we call it as Protocol Oriented Programming.

Question 1: Do we need to must and should implement all protocol methods in inherited class ?

Answer is No , we need not to we can make some methods optional , so it will be based on inherited class to implement it or not. Let’s see an example how to do it

@objc protocol P1 {

func printSum()

@objc optional func myOptionalMethods()

}

By mentioning a method along with @objc and optional , we are saying myOptionalMethods is an optional method.

Note: if any one of the method is optional then protocol also needs to be mentioned as @objc.

Question 2: Can we extend a Protocol ?

Answer is Yes, by using extension key we can extend protocol functionalities

extension P2 {

func printMultiplication() {

print(“multiplication”)

}

}

With the above examples , I covered what is POP , how to declare it and why do we need it. I hope this helps someone.

Please go through my other articles , thanks for reading. Happy Coding and Reading :).

https://medium.com/@kn.lakshmi948/what-are-optional-chaining-optional-binding-and-use-of-guard-key-in-swift-55b4ce90aad9

--

--