DEV Community

Visakh Vijayan
Visakh Vijayan

Posted on

5

Let's Interface

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.

  1. Suppose you want a device that you have made to be able to interact with your computer.
  2. You plug in your device using the USB-port. The computer says it doesn't recognize the device.
  3. You ask your computer, why don't you.
  4. The computer says it needs a couple of information from you to recognize this device.
  5. You tell the computer that tell me what you need and I will give you the information.
  6. 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?

  7. 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?

  8. Let's write it in the computer's language, shall we?
    a. fetchDeviceName()
    b. fetchDeviceWork()
    c. sizeOfDevice()
    d. sendOutput()
    e. shutdown()

  9. 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!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more