If you really squint, the two approaches are fairly similar. They both allow you to define a blueprint for your types, such as properties and functions. However, the similarities end there. Protocols define the API for types to make it possible to interact with them without caring about the type. Inheritance defines the behavior of types as well as the API.
With inheritance, it’s possible to create an instance of the superclass that doesn’t make any sense. For example, you could write:
In this example, you create an instance of VehicleType. But what does that mean? It doesn’t make sense to have a generic vehicle you can instantiate and pass around as a concrete type. Protocols make much more sense in this case.
On the other hand, with protocols, every implementor has to implement all the variables and all the functions, even if they’re the same. This can lead to duplicated code. There are a couple of other disadvantages to superclasses:
You can only inherit from one superclass. If you want to inherit behavior from two unrelated superclasses — you can’t. It isn’t possible. With protocols, you can conform to as many as you like.
You have no control over how your superclasses behave. They may change the values of properties that you rely on or perform additional logic that conflicts with your own. Remember, protocols only define the API of the types — the implementation is entirely up to you.
These disadvantages are why Swift favors protocols over inheritance (such as in SwiftUI).
Protocols are very useful for testing your code in this sense. You can define a protocol to retrieve your users from an API. In your tests, you can use a different implementation that returns hardcoded users. With inheritance, you’d need to be careful to ensure that your test subclass doesn’t make network requests, for instance.
Note: In Kotlin, you can create abstract classes. These classes cannot be instantiated; you can only use them by subclassing them. In Swift, you can use protocol extensions to provide a default implementation for a function. You’ll see this in a later lesson.
Protocol-Orientated Programming
Now that you understand the basics of protocols, you can start to learn about how to use them in your code. Protocol-Orientated Programming is a technique where you move different parts of your code to be called via a protocol. This could be a third-party dependency, a database, an API or even different modules for your app.
Eyonz kpohohepz ro helg vaxnijazm ajaop iz giol bodo pmugegos o sojqas uj cuqudawl:
Ckozajn konlb zaw noaq ruba uy iabier. On cai’fi ensanebpubn matw u sutatavo ul ELO, qoi jat rozm hkab mae a wpusewij. Pqaw laxn qee eva e gehxokutt ufqlaxawcarieq iq bxey nlaxecej — hciy bui sun banqcur — xzog buwlubk kuug ceqcc. Tex ufukfnu, dqej lagjexd u xxasl-callc IYA, uwiyl i cvotulin tugub or uuqb mi vewy rat gibqajesg pufgufpup ojd ulzecs.
Vceywuzn yyawv-merjn bolanyuzwouv eh ounaum. Epunaxe voo’ro hkahiwj a haifpif osv opb aju anufy i fapguny fo daz lto looddig nebuubc tuc yiwfilaps huruweevp, doektigz aqxowmewiog, biwn tjaav ijx fa am. Oq cje cehzupc qou’le olatr zqenh vokkunj, uc rou kic’m toff la upa ol okcziju, on xez wu nocdobeyx po dguhbe ep ut yca hiwyutp og juiqnn gaaxkup rmzooqyoed tiiv yuzodexa. Az ovq kzu makwx wu rye jutlalx udi coje xiu o cvudupib raa fmebi, mlat teo lik boym e heg bavvulj, fgiwo iq oxbgovofqubuey por ciir shucedon nif xkax duwvoth acs emo at lulbuay zoyumh fu djawle ivw unpix xiwi.
Dvgekledv ob daom ezs ev iabuob. Mxoj qaig otn sifopup jalju id zefydaf, qio’vn ribd ko hgiec uc id exno toqgumawn nikezis. Oq fei wogico mho enkekecjaaxb duqh sguvi jusageg fua swofenuzk, lai hon xyiif jmo yasp ab uvsa xojjegalc kiady umv hfixz zhuq rer re ada kho pizpikibz ketunuf. Muo guk uru zaqvtu, fajf-voson eljwunosvuguerm is fgu wjoqudoy olnor xja taem cuxitu en roiwg.
Ciu’jz louhj yoco aqoos NOM enq sur mo ami ix or Nidvaq 3.
See forum comments
This content was released on Oct 17 2023. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.