Just like a class is a blueprint of an object, an interface is a blueprint of a class. Whenever you are building software you can put in the demands that a class must have.
Let's understand this with an example.
- Suppose you want a device that you have made to be able to interact with your computer.
- You plug in your device using the USB-port. The computer says it doesn't recognize the device.
- You ask your computer, why don't you.
- The computer says it needs a couple of information from you to recognize this device.
- You tell the computer that tell me what you need and I will give you the information.
The computer says, I need the following:
a. What is your device's name?
b. What does it do?
c. What is the size of your device?
d. Can it send signals to the human using it?
e. What do I have to do to shut it down?
Look at point 6 closely. All the above things are common for any kind of device that you want to attach to your computer. What if all of these could be functions that the computer could call?
Let's write it in the computer's language, shall we?
Point 8 is what the computer needs. This is what an interface really is. The computer tells you that if you want me to recognize your device you have to give me access to such methods in your class. So your device will have all these methods in its class. (class implements an interface).
So you see, an interface is more or less like an agreement between two parties, in this case, two devices which want to communicate. The class implements the interface allowing smooth functioning. Tomorrow you come with a new device, you still have to follow the same approach to make your computer understand it.
No matter who you are, if you wanna talk to me, you talk to me on my terms. Thug Life ain't it!