DEV Community

Cover image for Why you actually need interfaces in the first place ( Dummies Guide)
Igadwa Magomere
Igadwa Magomere

Posted on

Why you actually need interfaces in the first place ( Dummies Guide)

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%"
    }
}
Enter fullscreen mode Exit fullscreen mode
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
    }
}

Enter fullscreen mode Exit fullscreen mode

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()
}
Enter fullscreen mode Exit fullscreen mode

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%"
    }
}
Enter fullscreen mode Exit fullscreen mode

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
    }
}
Enter fullscreen mode Exit fullscreen mode

This comes with far more advantages too:

  • including less coupling between the Person class and the GasGenerator class

Thank you for your time!

Free Vector | Worker with doubts

Download this Free Vector about Worker with doubts, and discover more than 65 Million Professional Graphic Resources on Freepik. #freepik #vector #businessmancartoon #entrepreneur #businesscartoon

favicon freepik.com

Top comments (0)