<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eugene Kovs</title>
    <description>The latest articles on DEV Community by Eugene Kovs (@kovs705).</description>
    <link>https://dev.to/kovs705</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1454215%2F0b766128-86c9-4bef-bece-2777c5c8855d.jpeg</url>
      <title>DEV Community: Eugene Kovs</title>
      <link>https://dev.to/kovs705</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kovs705"/>
    <language>en</language>
    <item>
      <title>Abstraction in Swift: A Comparative Look at Kotlin and Swift</title>
      <dc:creator>Eugene Kovs</dc:creator>
      <pubDate>Thu, 06 Feb 2025 14:18:35 +0000</pubDate>
      <link>https://dev.to/kovs705/abstraction-in-swift-a-comparative-look-at-kotlin-and-swift-4ole</link>
      <guid>https://dev.to/kovs705/abstraction-in-swift-a-comparative-look-at-kotlin-and-swift-4ole</guid>
      <description>&lt;p&gt;Object-oriented programming (OOP) is built upon core principles such as abstraction, encapsulation, inheritance, and polymorphism. These concepts often surface in technical interviews and discussions with peers, yet their practical use can sometimes be elusive in real projects.&lt;/p&gt;

&lt;p&gt;During the development of 11 screens in one of my projects, I encountered repetitive code. Specifically, the same observe method for handling Kotlin Multiplatform’s StateFlow was reimplemented across several view models. This redundancy signaled the need for abstraction: by extracting common logic into a single module, we could both eliminate duplication and ease code maintenance. In this article, I’ll explore the concept of abstraction and demonstrate how it’s implemented in Kotlin and Swift, highlighting their similarities and differences along the way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstraction in Kotlin and Swift
&lt;/h3&gt;

&lt;p&gt;At its core, abstraction involves &lt;strong&gt;hiding implementation details&lt;/strong&gt; while exposing only the necessary functionality. This separation simplifies interactions with objects and reduces code complexity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Abstraction in Kotlin
&lt;/h3&gt;

&lt;p&gt;In Kotlin, abstraction is achieved using abstract classes and interfaces.&lt;/p&gt;

&lt;h5&gt;
  
  
  Abstract Classes:
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;An abstract class can contain both &lt;em&gt;abstract methods&lt;/em&gt; (methods without an implementation) and &lt;em&gt;concrete methods&lt;/em&gt; (methods with an implementation). &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AbstractClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;abstractMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;printThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Implementation"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AbstractClass&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;abstractMethod&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;printThis&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Calls the already implemented method&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Rules for Abstract Classes in Kotlin:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;An abstract class in Kotlin is designed exclusively for inheritance and &lt;strong&gt;cannot be instantiated&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It can contain both abstract methods (methods without an implementation) and concrete methods (methods with an implementation).&lt;/li&gt;
&lt;li&gt;Abstract classes are used to provide a common interface and implementation for their subclasses.&lt;/li&gt;
&lt;li&gt;When a subclass inherits from an abstract class, it must provide implementations for all abstract methods defined in the abstract class. If a subclass does not implement all abstract methods, it must also be declared as abstract.&lt;/li&gt;
&lt;/ol&gt;

&lt;h5&gt;
  
  
  Interfaces
&lt;/h5&gt;

&lt;blockquote&gt;
&lt;p&gt;Kotlin interfaces, on the other hand, serve as a contract that contains only abstract methods and cannot have a state (fields). For example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Here, we defined &lt;code&gt;TestInterface&lt;/code&gt; as a contract for &lt;code&gt;TestClass&lt;/code&gt;, which requires now the implementation of &lt;code&gt;doIt()&lt;/code&gt; method inside.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;A More Detailed Example:&lt;/strong&gt;&lt;br&gt;
Consider a scenario where you have a &lt;code&gt;Person abstract class&lt;/code&gt; with both &lt;em&gt;abstract and concrete methods&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Abstract class&lt;/span&gt;
&lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Abstract method&lt;/span&gt;
    &lt;span class="k"&gt;abstract&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;birthDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Non-abstract method&lt;/span&gt;
    &lt;span class="k"&gt;open&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;personDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Person's Name: $name"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Age: $age"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Subclass inheriting from the abstract class&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Employee&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="py"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Implementing the abstract method birthDate()&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;birthDate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Birth Date: $date"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Overriding the personDetails() method&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;personDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Calling the personDetails method from the abstract class&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;personDetails&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Salary: $salary"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We create an &lt;code&gt;abstract class Person&lt;/code&gt; and add a &lt;code&gt;constant name&lt;/code&gt; and a &lt;code&gt;variable age&lt;/code&gt; to the constructor. The &lt;code&gt;name&lt;/code&gt; will remain unchanged, while the employee’s &lt;code&gt;age&lt;/code&gt; will change in the future, so we define it as &lt;code&gt;var&lt;/code&gt;. At the same time, we encapsulate these properties to restrict access to them only within the abstract class.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The implementation of the &lt;code&gt;birthDate method&lt;/code&gt; will vary depending on the subclass, so we use the &lt;code&gt;abstract keyword&lt;/code&gt; to enforce its implementation in child classes. The &lt;code&gt;personDetails() method&lt;/code&gt; already has a body, but we may want to extend it in subclasses. To allow this, we mark it with the &lt;code&gt;open keyword&lt;/code&gt;. Additionally, we introduce an internal &lt;code&gt;salary variable&lt;/code&gt; in the constructor of the child class and display the salary information after printing the basic employee details.&lt;/p&gt;

&lt;h5&gt;
  
  
  Advantages and Disadvantages of Abstract Classes in Kotlin:
&lt;/h5&gt;

&lt;h6&gt;
  
  
  Advantages
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Abstract classes allow defining common functionality that can be shared among multiple subclasses.&lt;/li&gt;
&lt;li&gt;They can contain both abstract methods, which must be implemented in subclasses, and concrete methods with implementations that can be overridden. However, in Kotlin (unlike Java), functions are final by default, so if you expect a method to be overridden, you must explicitly mark it as open.&lt;/li&gt;
&lt;li&gt;Abstract classes can maintain state through fields and properties that can be inherited and used by subclasses.&lt;/li&gt;
&lt;li&gt;They serve as a contract for subclasses, defining which methods must be implemented.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  Disadvantages
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;A class can inherit from only one abstract class, whereas it can implement multiple interfaces.&lt;/li&gt;
&lt;li&gt;Abstract classes cannot be instantiated directly and are meant solely for inheritance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h6&gt;
  
  
  When to Use What?
&lt;/h6&gt;

&lt;ul&gt;
&lt;li&gt;Use interfaces when you need &lt;em&gt;to define a set of rules that classes must follow&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;Use abstract classes when you need &lt;em&gt;to define shared logic&lt;/em&gt; that subclasses can inherit and extend.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Abstraction in Swift
&lt;/h3&gt;

&lt;p&gt;Unlike some other programming languages, such as Kotlin or Java, Swift does not have the concept of abstract classes. Instead, Swift follows an approach known as &lt;em&gt;protocol-oriented programming (POP)&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Swift, abstraction is typically implemented using &lt;strong&gt;protocols&lt;/strong&gt;. A protocol defines a set of methods and properties that a class, structure, or enumeration must implement. This allows us to establish a common structure for different types without concerning ourselves with their specific implementations.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Protocols
&lt;/h3&gt;

&lt;p&gt;Protocols in Swift are similar to interfaces in other programming languages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They &lt;em&gt;define a set of methods and properties&lt;/em&gt; that any conforming type must implement.&lt;/li&gt;
&lt;li&gt;Protocols &lt;em&gt;can be adopted&lt;/em&gt; by classes, structures, and enumerations, making them a flexible tool.&lt;/li&gt;
&lt;li&gt;Protocols &lt;em&gt;can be extended&lt;/em&gt; to provide default implementations, making methods or properties “optional.” This means that types conforming to the protocol can either provide their own implementations or use the default ones (via an extension, as shown below).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, you can define a protocol called &lt;code&gt;Drawable&lt;/code&gt;, which requires a type to implement a &lt;code&gt;draw() method&lt;/code&gt;. Any class, structure, or enumeration that conforms to the &lt;code&gt;Drawable protocol&lt;/code&gt; can be considered &lt;code&gt;drawable&lt;/code&gt;. Additionally, &lt;em&gt;protocols can inherit from other protocols&lt;/em&gt;, allowing you to build more complex abstractions from simpler ones.&lt;/p&gt;

&lt;p&gt;Here’s an example of a protocol with a default method implementation inside an extension in Swift:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;Drawable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;draw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;Drawable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;draw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Default drawing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Circle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Drawable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// We can provide a custom implementation of the method &lt;/span&gt;
    &lt;span class="c1"&gt;// or use the existing one from the Drawable extension.&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;draw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Drawing a circle"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Square&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Drawable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// This class will use the default implementation&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;draw&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;Circle&lt;/code&gt; provides its own implementation of the &lt;code&gt;draw() method&lt;/code&gt;, while &lt;code&gt;Square&lt;/code&gt; uses the &lt;strong&gt;default implementation&lt;/strong&gt;. Both types are considered &lt;code&gt;Drawable&lt;/code&gt;, even though they provide different implementations of this protocol.&lt;/p&gt;

&lt;h3&gt;
  
  
  Protocol and Interface Extensions
&lt;/h3&gt;

&lt;p&gt;Swift’s protocol extensions let you add method implementations that conforming types can use, similar to Kotlin’s default methods in interfaces. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Default implementation"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;MyClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// If no implementation is provided here, MyClass uses the default from the extension.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One nuance in Swift is that if you implement a method in your class that’s also provided by a &lt;code&gt;protocol extension&lt;/code&gt;, the compiler will use your class’s version without requiring an &lt;code&gt;override&lt;/code&gt; keyword. This behaviour can sometimes &lt;em&gt;reduce clarity because it isn’t immediately obvious that a default implementation exists&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Extending Interfaces and Protocols
&lt;/h3&gt;

&lt;p&gt;In Kotlin, interfaces can be extended in a way similar to Swift:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Extension function for the Test interface&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Test&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Outputs "Hello"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we first declare an &lt;code&gt;interface&lt;/code&gt; with the &lt;code&gt;doIt()&lt;/code&gt; method and then define an &lt;code&gt;extension function make()&lt;/code&gt; on the interface. &lt;em&gt;This function simply calls &lt;code&gt;doIt()&lt;/code&gt; from the interface&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;However, it’s important to note that &lt;strong&gt;the class must implement the interface in order to call &lt;code&gt;make()&lt;/code&gt;&lt;/strong&gt;. This means all required methods, in this case, &lt;code&gt;doIt()&lt;/code&gt;, must be implemented in the class.&lt;/p&gt;

&lt;p&gt;Starting from Java 8, interfaces can include method implementations using the default keyword:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;MyInterface&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// Declaring a default method  &lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;defaultMethod&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// Default method implementation  &lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;  
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Kotlin, there is no need to use the default keyword, making the syntax simpler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;makeIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;However, it is important to note that &lt;em&gt;writing implementations inside interfaces should generally be avoided&lt;/em&gt;, as interfaces are meant to &lt;strong&gt;define contracts&lt;/strong&gt;, not to provide specific implementations. This is the fundamental idea behind interfaces in object-oriented programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Inheritance in Swift
&lt;/h3&gt;

&lt;p&gt;Now, let’s take a look at how to create a default implementation in Swift protocols.&lt;/p&gt;

&lt;p&gt;First, we define a protocol &lt;code&gt;TestProtocol&lt;/code&gt; with the &lt;code&gt;doIt()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if we implement a class that conforms to this protocol, &lt;em&gt;we must define this function with its own implementation inside the class&lt;/em&gt;. This is straightforward. &lt;br&gt;
However, since &lt;strong&gt;Swift protocols are extendable&lt;/strong&gt;, we can move the implementation outside the class by using an extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// Default implementation  &lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While writing this code, you may notice that there is no autocomplete for &lt;code&gt;doIt()&lt;/code&gt;, and even more—&lt;em&gt;Xcode did not initially show an error if a class failed to implement all required protocol methods&lt;/em&gt;. It was only with Xcode 9 (2017, during the era of watchOS 4 and iOS 11) that an error message was introduced for cases where a class does not provide implementations for all protocol methods.&lt;/p&gt;

&lt;p&gt;It turns out that in Swift (or rather, in Xcode, as we’ll see later), we are not exactly implementing a method from the protocol. Instead, we are &lt;em&gt;“tracing over the original design like a stencil,”&lt;/em&gt; rather than directly inheriting it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfnup1srvz08ppjj0y1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjfnup1srvz08ppjj0y1k.png" alt="Lack of code-autocomplete in protocol extension" width="406" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Lack of code-autocomplete in protocol extension&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, in &lt;strong&gt;AppCode by JetBrains&lt;/strong&gt;, when writing the same code, &lt;em&gt;autocomplete is available&lt;/em&gt;:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkndw8wj22xiti9at25vs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkndw8wj22xiti9at25vs.png" alt="Screenshot of AppCode IDE" width="594" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Screenshot of AppCode IDE&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In an extension, you can also freely define a new method with an implementation without declaring it in the protocol:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"New method implementation"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When attempting to provide a custom implementation in a subclass, despite the existence of a default method, we will also notice &lt;strong&gt;the absence of autocomplete&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhbbabs4l9kf78aadykq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyhbbabs4l9kf78aadykq.png" alt="Absence of autocomplete" width="255" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here, there will be no override keywords and no autocompletions.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We can use the existing implementation, but if we want to write our own, &lt;em&gt;we must precisely match the function’s name and signature&lt;/em&gt;. Otherwise, we may encounter &lt;strong&gt;issues&lt;/strong&gt; such as code &lt;strong&gt;duplication or calling the wrong method&lt;/strong&gt;:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4drogdoq39erpravhc5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu4drogdoq39erpravhc5.png" alt="Here, the method makeIt() uses the implementation defined within the class." width="267" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here, the method &lt;strong&gt;makeIt()&lt;/strong&gt; uses the implementation defined &lt;strong&gt;within the class&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Only by doing it exactly this way will we replace the function with our own implementation, &lt;em&gt;discarding &lt;code&gt;makeIt()&lt;/code&gt; from the &lt;code&gt;protocol extension&lt;/code&gt;&lt;/em&gt;. If we mistype even a single character and fail to verify which function is actually being called, &lt;strong&gt;code duplication may occur&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Going back to Kotlin, if we want to write our own implementation of a method despite an existing one in the interface, in addition to autocomplete, we get the following behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Hello"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TestClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TestInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// This calls the method defined in the interface&lt;/span&gt;
        &lt;span class="c1"&gt;// Here, we can add our own implementation:&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Bye!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;By simply typing &lt;code&gt;doIt()&lt;/code&gt; and using autocomplete, we not only eliminate potential errors, but also automatically include a &lt;code&gt;super.doIt()&lt;/code&gt; call within our override as a bonus!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, an exception to this rule is interface extensions in Kotlin:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzzjzdbt8f479j3qrrdg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frzzjzdbt8f479j3qrrdg.png" alt="Same lack of autocomplete" width="307" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here, we have written an extension for the &lt;code&gt;testInt interface&lt;/code&gt; with the method &lt;code&gt;makeIt()&lt;/code&gt;. If we insert it in place of &lt;code&gt;TODO&lt;/code&gt;, the call will be &lt;em&gt;successful&lt;/em&gt;. However, autocomplete for this method is not available inside &lt;code&gt;TestClass&lt;/code&gt;. Instead, we can create a new method with the same name, and the compiler will then determine which &lt;code&gt;makeIt()&lt;/code&gt; implementation to use inside &lt;code&gt;TestClass&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point, the following questions arise regarding Swift and Xcode:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If we declare a function with the same name inside a class, &lt;em&gt;which function will be called&lt;/em&gt;? How does the compiler determine which function implementation to use?&lt;/li&gt;
&lt;li&gt;Where is the &lt;em&gt;&lt;code&gt;override&lt;/code&gt; keyword before &lt;code&gt;func&lt;/code&gt;&lt;/em&gt;, as in class inheritance?
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Will this one be called?&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ViewController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UIViewController&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Or will this one be called?&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Answering the Newly Raised Questions:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;doIt()&lt;/code&gt; is implemented &lt;strong&gt;inside the class&lt;/strong&gt;, that version &lt;em&gt;will be called&lt;/em&gt;. Otherwise, the function from the &lt;code&gt;protocol extension&lt;/code&gt; will be used. The compiler first checks for an implementation &lt;strong&gt;inside the class&lt;/strong&gt;, and if it doesn’t find one, it falls back to the &lt;code&gt;protocol extension&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In Swift, when working with protocols, there is a mechanism called the &lt;strong&gt;Witness Table&lt;/strong&gt;. Essentially, it is a &lt;strong&gt;data structure&lt;/strong&gt; that maps each method or property requirement in a protocol to its &lt;strong&gt;corresponding implementation&lt;/strong&gt; in the conforming type.&lt;/p&gt;

&lt;p&gt;For example, let’s consider &lt;em&gt;two protocols&lt;/em&gt; and &lt;em&gt;two classes&lt;/em&gt;, along with their mappings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;FirstProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;SecondProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;OurClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;FirstProtocol&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;SecondProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation is required&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;makeIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation of the method from the second protocol&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SecondClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;FirstProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;doIt&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Implementation is required here as well&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1err3khobq0al40w0vpb.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1err3khobq0al40w0vpb.jpeg" alt="Witness Table representation" width="800" height="861"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Witness Table representation&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Please note the &lt;strong&gt;bolded memory addresses&lt;/strong&gt; for the methods in the two classes. The address for &lt;code&gt;FirstProtocol&lt;/code&gt; remains the same across different implementations.&lt;/p&gt;

&lt;p&gt;This means that for &lt;strong&gt;each protocol adopted by a class&lt;/strong&gt;, a &lt;strong&gt;separate table is created&lt;/strong&gt;, mapping the protocol’s methods to their respective implementations. Even though the &lt;strong&gt;protocols themselves remain identical&lt;/strong&gt;, their method &lt;strong&gt;implementations differ&lt;/strong&gt; across conforming classes.&lt;/p&gt;

&lt;p&gt;Thus, we can conclude that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every method defined in a protocol must either be explicitly implemented or have a default implementation provided in a protocol extension.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the earlier example, when we defined our own &lt;code&gt;makeIt()&lt;/code&gt; method inside the class, the compiler first checked whether &lt;code&gt;makeIt()&lt;/code&gt; existed within the &lt;em&gt;class itself&lt;/em&gt;. Since it found the method, it did not associate it with the &lt;code&gt;protocol&lt;/code&gt;, and &lt;em&gt;static dispatching&lt;/em&gt; was more likely to be used instead of the &lt;em&gt;Witness Table&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;However, it is also important to note that if a method is implemented in a &lt;code&gt;protocol extension&lt;/code&gt;, it is most likely to be handled via static dispatching as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where is &lt;code&gt;override&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;Now, let’s move on to the second question: Where is the &lt;code&gt;override&lt;/code&gt; keyword before &lt;code&gt;func&lt;/code&gt;, as in classes?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Swift, the &lt;code&gt;override&lt;/code&gt; keyword is used when &lt;strong&gt;overriding a method or property from a parent class&lt;/strong&gt; in a &lt;code&gt;subclass&lt;/code&gt;. This keyword ensures that the parent class actually contains the method that the subclass is attempting to override.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;However, in the context of &lt;code&gt;protocol extensions&lt;/code&gt;, when we provide a “&lt;em&gt;default implementation&lt;/em&gt;” for a protocol method in an extension, overriding this method inside a conforming class &lt;strong&gt;does not require the override keyword&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This can be subjectively problematic for code readability because without the &lt;code&gt;override&lt;/code&gt; keyword, it becomes harder to determine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Whether a method has a default implementation in a protocol extension.&lt;/li&gt;
&lt;li&gt;Whether the method belongs to a protocol at all.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Inheritance
&lt;/h2&gt;

&lt;p&gt;Now, this smoothly brings us back to the topic of &lt;code&gt;abstract classes&lt;/code&gt;, which we previously explored using &lt;strong&gt;Kotlin&lt;/strong&gt; as an example.&lt;/p&gt;

&lt;p&gt;One of the fundamental principles of &lt;strong&gt;OOP&lt;/strong&gt; is &lt;strong&gt;Inheritance&lt;/strong&gt;. A classic example is the &lt;em&gt;Car parent class&lt;/em&gt;, with &lt;em&gt;Motorcycle&lt;/em&gt; and &lt;em&gt;Truck&lt;/em&gt; as child classes.&lt;/p&gt;

&lt;p&gt;All of these classes share common functionality, such as driving or honking, inherited from the &lt;strong&gt;Car class&lt;/strong&gt;. At the same time, they each have their own unique methods and properties, depending on their characteristics.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A motorcycle cannot function as a dump truck.&lt;/li&gt;
&lt;li&gt;A truck cannot easily maneuver through traffic like a motorcycle... at least theoretically..&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s assume we have a parent class and add two arbitrary functions to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Eating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  

    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;drink&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Drinking"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we already know, we can now create child classes that inherit from the parent class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt; classes look identical, but now we can add new independent methods and properties to the child classes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;playGames&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;buyFood&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition to adding new properties or methods, we also have the option to override existing ones that were declared in the parent class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Eating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"NOT EATING"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;To prevent accidental method overrides&lt;/strong&gt; in a subclass, we can add the &lt;code&gt;final&lt;/code&gt; keyword before the function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Eating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="c1"&gt;// Error  &lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;The &lt;code&gt;final&lt;/code&gt; keyword prevents the &lt;code&gt;eat()&lt;/code&gt; method from being overridden in any subclass. Attempting to override it will result in a &lt;strong&gt;compiler error&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Additionally, &lt;code&gt;final&lt;/code&gt; can also be placed before &lt;code&gt;class&lt;/code&gt; in the parent class, &lt;strong&gt;completely prohibiting inheritance&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;eat&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Eating"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// This will cause an error:&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ChildClassA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thus, based on the principles of &lt;strong&gt;inheritance&lt;/strong&gt; and &lt;strong&gt;polymorphism&lt;/strong&gt;, we can create our own &lt;code&gt;abstract class&lt;/code&gt; (even without the &lt;code&gt;abstract&lt;/code&gt; keyword) by providing implementations for common methods that will be used in the child classes.&lt;/p&gt;

&lt;p&gt;Unlike abstract class in &lt;strong&gt;Kotlin&lt;/strong&gt;, we won’t have all its features, such as the &lt;strong&gt;inability to instantiate an abstract class&lt;/strong&gt;. However, implementing abstract classes in Swift still allows us to define a common structure and behavior for subclasses, while also enabling unique implementations in each subclass as needed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summarising the First Part: Inheritance Limitations in Swift
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Restriction on Multiple Inheritance
Unlike some other programming languages, such as C++ or Python, &lt;strong&gt;Swift does not support multiple inheritance for classes&lt;/strong&gt;. This means that a class in Swift can &lt;strong&gt;inherit from only one parent class&lt;/strong&gt;:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SecondParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;FirstSubclass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;SecondParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// Error  &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Structures and Enumerations Cannot Inherit from Classes or Each Other
In Swift, structs and enums cannot inherit from classes or other structs/enums. This is an intentional language restriction to ensure type safety and simplify inheritance:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;  

&lt;span class="kd"&gt;struct&lt;/span&gt; &lt;span class="kt"&gt;SubStruct&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="c1"&gt;// Error  &lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Restriction on Inheriting from Protocols
In Swift, classes, structures, and enumerations can conform to multiple protocols, but they &lt;strong&gt;cannot inherit method implementations from these protocols&lt;/strong&gt;.
Instead, every method defined in a protocol must either:&lt;/li&gt;
&lt;li&gt;Be explicitly implemented in the conforming type, or&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Have a default implementation provided in a protocol extension.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Restriction on Method Overriding&lt;br&gt;
In Swift, methods defined in a superclass can be overridden in a subclass using the &lt;code&gt;override&lt;/code&gt; keyword. However, a &lt;strong&gt;subclass cannot override methods that were defined in a protocol extension&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;protocol&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;extension&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Printing it"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;TestProtocol&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Inherits the default implementation from the protocol extension&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;SubClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ParentClass&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;method&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Error: Method does not override any method from its superclass&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course, we could explicitly define &lt;code&gt;method()&lt;/code&gt; inside &lt;code&gt;ParentClass&lt;/code&gt;, but that would be a &lt;strong&gt;direct implementation&lt;/strong&gt;, not an &lt;code&gt;override&lt;/code&gt;...&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;I hope this section has helped clarify the theoretical aspects of inheritance, method overriding, and working with protocols in Swift and interfaces in Kotlin.&lt;/p&gt;

&lt;p&gt;In this article, we explored interfaces in Kotlin and protocols in Swift, examined some key differences and nuances, discussed inheritance, and reviewed various ways to implement abstraction in both programming languages.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Additional Resources&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://medium.com/better-programming/static-dispatch-over-dynamic-dispatch-a-performance-analysis-47f9fee3803a" rel="noopener noreferrer"&gt;Speed Difference Between Static Dispatch and Dynamic Dispatch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.baeldung.com/kotlin/interfaces" rel="noopener noreferrer"&gt;More About Interfaces in Kotlin and Potential Nuances&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Software versions I used for this article (as of December 2023):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;macOS Sonoma 14.1.1 (23B81)&lt;/li&gt;
&lt;li&gt;Xcode Version 15.1 (15C65) (and earlier versions starting with 15.0 RC)&lt;/li&gt;
&lt;li&gt;Android Studio Hedgehog | 2023.1.1&lt;/li&gt;
&lt;li&gt;Android Studio Giraffe | 2022.3.1 Patch 2&lt;/li&gt;
&lt;li&gt;AppCode 2023.1.4&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>swift</category>
      <category>multiplatform</category>
      <category>kotlin</category>
      <category>mobile</category>
    </item>
  </channel>
</rss>
