Why you actually need interfaces in the first place ( Dummies Guide)
Tags: beginners, android, kotlin
Published: 2023-07-31 08:57:20

## Why you actually need interfaces in the first place ( By Examples)

This is a more illustrative approach that elaborates the thinking behind interfaces and why they are useful.
We demonstrate interfaces being useful in a life or death situation.
So let's take an example of a person:
 - 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?

so let's turn that ideology into a kotlin program.
We'll have two scenarios:
 - One where we don't use interfaces.
 - Another that shows interfaces in use for survival.

## 1. First Scenario - One where we don't use interfaces.
> _**Note**:_ _I have added the types intentionally so that it's easier to grasp what kind of data will be passed
however without them, kotlin will still type them by inference._

```
class GasGenerator {
    var amountOfOxygenToGive: String = "0%"
    fun makeOxygen() {
        amountOfOxygenToGive = "21%"
    }
}
```


```
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
    }
}

```
Problems with the above approach:
 - assume the GasGenerator's implementation was changed so that it now produces carbon dioxide
 - 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.

## 2. Second Scenario - One that shows interfaces in use for survival of our sea diver.
For now, think of an interface as a hard contract that all people who have signed 
MUST adhere to at the very least doing what it says
in order to work normally.
In our case, we would like it to be that oxygen air be a must 
for every class that generates gas for our diver
So lets make the contract, shall we?

```
interface ConfirmBreathable {
    fun makeOxygen()
}
```

so now everyone who implements that "contract" ConfirmBreathable must atleast give our diver a means for breathing

```
class GasGenerator: ConfirmBreathable {
    var amountOfOxygenToGive: String = "0%"

    override fun makeOxygen() {
         amountOfOxygenToGive = "21%"
    }
}
```

Next we will pass the gas through the constructor of the person class. Why?
Notice how the type of the passed variable is actually ConfirmBreathable our interface "contract"?
This means that whenever someone uses our Person class they must provide a gas generator which implements
the ConfirmBreathable interface. This guarantees that wherever we are getting the gas from, that class for 
sure will contain at the very least a method for making oxygen for our divers.
In this way our diver will always have oxygen provided to them.

```
class Person(
    val breathableGas: ConfirmBreathable
) {
    fun take_A_DeepBreath() {
        //reduce contents of the oxygen cylinder as you inhale - Now provided by breathableGas variable
    }
}
```



This comes with far more advantages too:
 - including less coupling between the Person class and the GasGenerator class


Thank you for your time!


{% embed  https://www.freepik.com/free-vector/worker-with-doubts_834551.htm#query=cartoon%20character%20thinking&position=6&from_view=keyword&track=ais %}