Interface

Hello everybody! Good day, how are you? well i’m just fine. Last time we talked about abstraction, basically abstraction is a process that hides the details of the code from the users that use it, and will only show it’s functionality (you can see my last post about abstraction). An example of abstraction is like a postman, we don’t need to know how the postman will send our letter, we only knew that the postman will send us our letter. Today we’ll talk about Interface, because it’s still a part of Abstraction.

Do you wonder what is an Interface is? if you do, good thinking. Interfaces is a way to achieve a full abstraction, which is needed to hide anything that the users doesn’t need to know. But for now let’s get the meaning of Interface, can you tell me what it is? well more or less it’s like this :
Interface is a reference type in Java, it is similar to class but a bit different, an Interface is a collection of Abstraction or by default is abstract(it have an empty body and only a method signature). A class can implements more than one Interface, but a class can’t extends more than one class.

Just think of this, your tv power button. It’s an Interface between you and the electrical wire on the inside or in the other side of the tv, using that button you turn on and off the tv.
Let’s take a common example of an interface, basically interface is a group of related methods with empty bodies. Look at this bicycle’s behavior, if specified as an interface, the code might appear as follows:

 
interface Bicycle {

void changeGear(int newValue);

void speedUp(int increment);

void applyBrakes(int decrement);
  }

if you want to implements it to a new class bicycle, you can name the bicycle into anything you want, but for now let’s name it tandem bike. If you want to implement it to your tandem bike class you need to make a declaration of the interface after the class name itself, and if you put it into a working code, it would look like the following :

class TandemBike implements Bicycle {

int speed = 20;
int gear = 2;

/*The compiler will now require that methods
changeCadence, changeGear, speedUp, and applyBrakes
all be implemented. Compilation will fail if those
*/ methods are missing from this class.

void changeGear(int newValue) {
gear = newValue;
}

void speedUp(int increment) {
speed = speed + increment;
}

void applyBrakes(int decrement) {
speed = speed - decrement;
}

void printStates() {
System.out.println("speed: " +
speed + " gear: " + gear);
}
}

This will output the following of :

speed: 2 gear: 2

Conclusions

  •  Interfaces is a way to achieve a full abstraction.
  •  Interface is a reference type in Java, it is similar to class but a bit different, an Interface is a collection of Abstraction or by default is abstract(it have an empty body and only a method signature).
  •  A class can implements more than one Interface, but a class can’t extends more than one class. 
  • If you want to implement it to your tandem bike class you need to make a declaration of the interface after the class name itself.

One thought on “Interface”

Leave a Reply

Your email address will not be published. Required fields are marked *