<?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: Igadwa Magomere</title>
    <description>The latest articles on DEV Community by Igadwa Magomere (@m_igadwa).</description>
    <link>https://dev.to/m_igadwa</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%2F1129846%2Fe3f56396-5bf3-487c-9476-98abfd47454e.png</url>
      <title>DEV Community: Igadwa Magomere</title>
      <link>https://dev.to/m_igadwa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/m_igadwa"/>
    <language>en</language>
    <item>
      <title>Why you actually need interfaces in the first place ( Dummies Guide)</title>
      <dc:creator>Igadwa Magomere</dc:creator>
      <pubDate>Mon, 31 Jul 2023 08:57:20 +0000</pubDate>
      <link>https://dev.to/m_igadwa/why-you-actually-need-interfaces-in-the-first-place-dummies-guide-2261</link>
      <guid>https://dev.to/m_igadwa/why-you-actually-need-interfaces-in-the-first-place-dummies-guide-2261</guid>
      <description>&lt;h2&gt;
  
  
  Why you actually need interfaces in the first place ( By Examples)
&lt;/h2&gt;

&lt;p&gt;This is a more illustrative approach that elaborates the thinking behind interfaces and why they are useful.&lt;br&gt;
We demonstrate interfaces being useful in a life or death situation.&lt;br&gt;
So let's take an example of a person:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A person needs oxygen air for breathing, we need it lest we can't live.
Imagine you were going for a see deep dive, you need oxygen to breath under water, right?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;so let's turn that ideology into a kotlin program.&lt;br&gt;
We'll have two scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One where we don't use interfaces.&lt;/li&gt;
&lt;li&gt;Another that shows interfaces in use for survival.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  1. First Scenario - One where we don't use interfaces.
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note&lt;/strong&gt;:&lt;/em&gt; &lt;em&gt;I have added the types intentionally so that it's easier to grasp what kind of data will be passed&lt;br&gt;
however without them, kotlin will still type them by inference.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class GasGenerator {
    var amountOfOxygenToGive: String = "0%"
    fun makeOxygen() {
        amountOfOxygenToGive = "21%"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person {
    //define air for me to breath lest I exist not.
    val myOxygenGasCylinder: GasGenerator = GasGenerator()

    fun take_A_DeepBreath() {
        //reduce contents of the oxygen cylinder as you inhale
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Problems with the above approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;assume the GasGenerator's implementation was changed so that it now produces carbon dioxide&lt;/li&gt;
&lt;li&gt;that would be the end of the deep diver, right?
What if there was a way to make sure no matter what,
the gas generator will always atleast have a means to make oxygen for our diver to breath in?
A way to prevent the evil generator from not making oxygen for our diver?
That's where interfaces come in, let's see how interfaces solve this problem for us.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  2. Second Scenario - One that shows interfaces in use for survival of our sea diver.
&lt;/h2&gt;

&lt;p&gt;For now, think of an interface as a hard contract that all people who have signed &lt;br&gt;
MUST adhere to at the very least doing what it says&lt;br&gt;
in order to work normally.&lt;br&gt;
In our case, we would like it to be that oxygen air be a must &lt;br&gt;
for every class that generates gas for our diver&lt;br&gt;
So lets make the contract, shall we?&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface ConfirmBreathable {
    fun makeOxygen()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;so now everyone who implements that "contract" ConfirmBreathable must atleast give our diver a means for breathing&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class GasGenerator: ConfirmBreathable {
    var amountOfOxygenToGive: String = "0%"

    override fun makeOxygen() {
         amountOfOxygenToGive = "21%"
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Next we will pass the gas through the constructor of the person class. Why?&lt;br&gt;
Notice how the type of the passed variable is actually ConfirmBreathable our interface "contract"?&lt;br&gt;
This means that whenever someone uses our Person class they must provide a gas generator which implements&lt;br&gt;
the ConfirmBreathable interface. This guarantees that wherever we are getting the gas from, that class for &lt;br&gt;
sure will contain at the very least a method for making oxygen for our divers.&lt;br&gt;
In this way our diver will always have oxygen provided to them.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Person(
    val breathableGas: ConfirmBreathable
) {
    fun take_A_DeepBreath() {
        //reduce contents of the oxygen cylinder as you inhale - Now provided by breathableGas variable
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This comes with far more advantages too:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;including less coupling between the Person class and the GasGenerator class&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for your time!&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://www.freepik.com/free-vector/worker-with-doubts_834551.htm#query=cartoon%20character%20thinking&amp;amp;amp;position=6&amp;amp;amp;from_view=keyword&amp;amp;amp;track=ais" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--d-aUlWES--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://img.freepik.com/free-vector/worker-with-doubts_1012-193.jpg%3Ft%3Dst%3D1690792910%7Eexp%3D1690793510%7Ehmac%3D3651072ca18a5578c50394d5dea506da7d7945b12d2fa82b28a37cb7f7b2b62f" height="626" class="m-0" width="626"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://www.freepik.com/free-vector/worker-with-doubts_834551.htm#query=cartoon%20character%20thinking&amp;amp;amp;position=6&amp;amp;amp;from_view=keyword&amp;amp;amp;track=ais" rel="noopener noreferrer" class="c-link"&gt;
          Free Vector | Worker with doubts
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Download this Free Vector about Worker with doubts, and discover more than 65 Million Professional Graphic Resources on Freepik. #freepik #vector #businessmancartoon #entrepreneur #businesscartoon
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--c6pI1alT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://freepik.cdnpk.net/img/favicons/favicon.ico%3Fv%3D2018082101" width="256" height="256"&gt;
        freepik.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>beginners</category>
      <category>android</category>
      <category>kotlin</category>
    </item>
  </channel>
</rss>
