A protocol declares a programmatic interface that any class or structs or enums may choose to implement. It act as blueprints rather than parents.
POP is a new approach for programming where you decorate your classes, structs or enums using protocols. By default swift does not support multiple inheritance, therefore problem pops out when you want to add multiple abilities to your class. Protocol OP lets you to add new abilities to a class or struct or enum via protocols which can support multiple implementations.
For instance a ‘Car Engine’ protocol can implemented from ‘Petrol engine’’ and ‘Electric engine’ protocol.
Syntax :
What about Object Oriented Programming (OOP) ?
Creating abstract (methods without implementation) classes or base classes mainly done from inheritance. Every subclasses will have the ability of their super classes (unless ‘final’ method type ). A subclass can override that ability, add specific one. OOP works perfect till you need another ability from another class.
In OOP data is secured which we can not move easily from function to function. It can be kept public or private so we can control the access of data. Where us in POP, data is globally available and share via protocol.
This allows us to repeat a functionality without duplicating code. Further, we can also make clases confirming to a same protocol respond differently. In fact, POP and OOP approaches can co-exists in an application.