Interface

Beginner

A shared boundary across which two or more separate components of a system exchange information. This can be a contract between software components (OOP/API) or the point of interaction between a human and a computer (UI).

First Used

1960s

Definitions

3

Synonyms
ProtocolContractSpecification

Definitions

1

Interface in Object-Oriented Programming (OOP)

In Object-Oriented Programming (OOP), an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. It acts as a contract or a protocol that a class agrees to follow. A class that implements an interface must provide the concrete implementation for all the abstract methods declared in it. Interfaces cannot be instantiated directly.

Key Concepts:

  • Abstraction: Interfaces hide the implementation details and only expose the necessary functionalities. Consumers of the class only need to know about the interface methods, not how they are implemented.
  • Polymorphism: A variable of an interface type can hold a reference to any object of a class that implements that interface. This allows for writing more flexible and decoupled code.
  • Multiple Inheritance: While many languages (like Java) do not support multiple inheritance of classes to avoid the 'diamond problem', they allow a class to implement multiple interfaces. This allows a class to take on behaviors from several different contracts.

Example (Java):

// The interface (contract)
interface Drivable {
    void startEngine();
    void drive(int distance);
    void stopEngine();
}

// A class implementing the interface
class Car implements Drivable {
    @Override
    public void startEngine() {
        System.out.println("Car engine started.");
    }

    @Override
    public void drive(int distance) {
        System.out.println("Driving the car for " + distance + " miles.");
    }

    @Override
    public void stopEngine() {
        System.out.println("Car engine stopped.");
    }
}

// Usage
public class Main {
    public static void main(String[] args) {
        Drivable myCar = new Car(); // Polymorphism
        myCar.startEngine();
        myCar.drive(50);
    }
}
2

User Interface (UI)

A User Interface (UI) is the point of interaction and communication between a human user and a computer, application, or device. It encompasses all the visual elements (like buttons, icons, and screens), input methods (like keyboard, mouse, and touch), and feedback mechanisms (like sounds and vibrations) that allow a person to control and receive information from the system.

Key Concepts:

  • Graphical User Interface (GUI): The most common type of UI, which uses windows, icons, menus, and pointers (WIMP) for user interaction.
  • Command-Line Interface (CLI): A text-based interface where users type commands to interact with the system.
  • Usability: A measure of how easy and pleasant the UI is to use. Good UI design focuses on creating an intuitive, efficient, and satisfying user experience (UX).
3

Application Programming Interface (API)

An Application Programming Interface (API) is a set of rules, protocols, and tools for building software applications. It specifies how software components should interact. Essentially, an API is an interface for software, allowing different applications to communicate with each other without needing to know how they are implemented. For example, when you use an app on your phone to check the weather, the app uses a weather service's API to request and display the data.

Key Concepts:

  • Endpoints: Specific URLs where API requests are sent.
  • Requests/Responses: The mechanism of communication, typically using HTTP methods (GET, POST, PUT, DELETE) and data formats like JSON or XML.
  • Abstraction: Like an OOP interface, an API hides the complex internal logic of a service and exposes only the necessary functionality in a predictable way.

Origin & History

Etymology

The term 'interface' comes from the Latin prefix 'inter-', meaning 'between', and the word 'face', referring to a surface or boundary. It literally means a common boundary or surface between two separate regions, systems, or entities.

Historical Context

The concept of an 'interface' as a boundary between components has been fundamental to engineering for centuries. In computing, its importance grew with the advent of modular programming in the 1960s, where systems were broken down into smaller, interchangeable parts. The connections between these modules were their interfaces. The term was formalized in object-oriented programming with languages like Smalltalk in the 1970s, which emphasized message passing between objects. However, it was the Java programming language, released in 1995, that popularized the `interface` as a core language keyword and a central tool for achieving abstraction and a form of multiple inheritance. This solidified its role as a design **contract**. In parallel, the concept evolved in human-computer interaction, moving from physical punch cards to Command-Line Interfaces (CLIs) and then to the Graphical User Interface (GUI) pioneered by Xerox PARC in the 1970s and popularized by Apple and Microsoft in the 1980s. More recently, the rise of web services and microservices in the 2000s and 2010s has made the Application Programming Interface (API) a critical type of interface for modern software architecture.


Usage Examples

1

In our Java project, the PaymentProvider interface defines the charge() and refund() methods, ensuring any payment gateway class we write will have a consistent specification.

2

The design team is conducting usability tests on the new user interface to ensure it is intuitive for first-time users.

3

To fetch user data from the external service, our application needs to make a GET request to their public API.

4

By programming to an interface rather than a concrete class, we created a more modular system that is easier to test and maintain.


Frequently Asked Questions

What is the primary purpose of an interface in Object-Oriented Programming?

In OOP, an interface defines a contract of methods that a class must implement. Its primary purpose is to achieve abstraction and enable polymorphism, allowing for loosely coupled and flexible code.

How does a User Interface (UI) differ from an Application Programming Interface (API)?

A User Interface (UI) is for human-computer interaction, involving visual and input elements. An Application Programming Interface (API) is for software-to-software interaction, defining how different programs communicate.

What is the main difference between an interface and an abstract class?

An abstract class can have both abstract methods (without implementation) and concrete methods (with implementation), as well as instance variables. An interface traditionally only contains method signatures (a contract), although modern languages allow default methods. A class can extend only one abstract class but can implement multiple interfaces.


Categories

Object-Oriented ProgrammingSoftware DesignUser Experience

Tags

OOPAPIUIContractAbstractionPolymorphismSoftware Design