Implement An Interface

Not to be confused with a graphical interface, interface in OOP is a CONTRACT with a promise to fulfill BETWEEN two parties—a class and the outside world (its interface). When a class “implements” an interface it promises to provide the methods (behaviors) of that interface.

Methods form the object’s INTERFACE with other outside objects—the class’ API.

An interface is CREATED similar to a class but it uses the keyword interface instead of class:

interface MyBox {
void setHeight (int newHeight);
void setWidth (int newWidth);
void setColor (String newColor);
}

NOTES:
- All of the code from the class MyBox was deleted except the method signatures and the class definition has been changed to an interface definition.

QUESTION: Can you convert a class to an interface in Android Studio?

An interface is USED by using the keyword implements with the class you want to “implement.”

class MyCustomBox implements MyBox {
int height=100;
int width=100;
string color = “blue”;

void setHeight (int newHeight) {
height = newHeight;
}

void setWidth (int newWidth) {
height = newWidth;
}

void setColor (int newColor) {
color = newColor;
}

}

NOTE:  To actually compile the MyCustomBox class, you'll need to add the public keyword to the beginning of the implemented interface methods. This will be discussed later.

NOTES: