Interface (computer science)
Encyclopedia
In the field of computer science
, an interface is a tool
and concept
that refers to a point of interaction between components, and is applicable at the level of both hardware
and software
. This allows a component, whether a piece of hardware such as a graphics card
or a piece of software such as an Internet browser
, to function independently while using interfaces to communicate with other components via an input/output
system and an associated protocol.
In addition to hardware and software interfaces, a computing interface may refer to the means of communication between the computer and the user by means of peripheral
devices such as a monitor or a keyboard
, an interface with the Internet
via Internet Protocol
, and any other point of communication involving a computer.
devices, etc. A hardware interface is described by the mechanical, electrical and logical signals at the interface and the protocol for sequencing them (sometimes called signaling). A standard interface, such as SCSI
, decouples the design and introduction of computing hardware, such as I/O
devices, from the design and introduction of other components of a computing system, thereby allowing users and manufacturers great flexibility in the implementation of computing systems. Hardware interfaces can be parallel where performance is important or serial
where distance is important.
s or program
s running on the operating system may need to interact via streams, and in object oriented programs
, objects within an application may need to interact via methods
.
The types of access that interfaces provide between software components can include: constants, data type
s, types of procedure
s, exception
specifications and method signatures. In some instances, it may be useful to define public variables
as part of the interface. It often also specifies the functionality of those procedures and methods, either by comment
s or (in some experimental languages) by formal logical assertions and preconditions.
The interface of a software module is deliberately kept separate from the implementation of that module. The latter contains the actual code of the procedures and methods described in the interface, as well as other "private" variables, procedures, etc.. Any other software module (that can be referred to as a client to A) that interacts with is forced to do so only through the interface. One practical advantage of this arrangement is that replacing the implementation of by another one that meets the same specifications of the interface should not cause to fail—as long as its use of complies with the specifications of the interface. (See also Liskov substitution principle
.)
that contains no data, but exposes behaviors defined as methods
. A class
having all the methods corresponding to that interface is said to implement that interface. Furthermore, a class can implement multiple interfaces, and hence can be of different types at the same time.
An interface is hence a type
definition; anywhere an object can be exchanged (in a function or method
call) the type of the object to be exchanged can be defined in terms of an interface instead of a specific class
. This allows later code to use the same function exchanging different object types; hence such code turns out to be more generic and reusable.
Usually a method in an interface cannot be used directly; there must be a class implementing that object to be used for the method invocation. For example, one can define an interface called "Stack
" that has two methods:
This approach can be pushed to the limit of defining interfaces with a single method; e.g. the Java
language defines the interface Readable that has the single
like contain virtually nothing.
In its purest form, an interface (like in Java) must include only method definitions and constant
values that make up part of the static interface of a type.
Some languages (like C#) also permit the definition to include properties owned by the object, which are treated as methods with syntactic sugar
, but no constants (in contrast to Java), constructors, destructors, fields, nested types, or operators.
Pushing this idea to the limit one can introduce the inversion of control
that means leaving the context to inject the code with the specific implementations of the interface that will be used to perform the work.
Computer science
Computer science or computing science is the study of the theoretical foundations of information and computation and of practical techniques for their implementation and application in computer systems...
, an interface is a tool
Tool
A tool is a device that can be used to produce an item or achieve a task, but that is not consumed in the process. Informally the word is also used to describe a procedure or process with a specific purpose. Tools that are used in particular fields or activities may have different designations such...
and concept
Concept
The word concept is used in ordinary language as well as in almost all academic disciplines. Particularly in philosophy, psychology and cognitive sciences the term is much used and much discussed. WordNet defines concept: "conception, construct ". However, the meaning of the term concept is much...
that refers to a point of interaction between components, and is applicable at the level of both hardware
Computer hardware
Personal computer hardware are component devices which are typically installed into or peripheral to a computer case to create a personal computer upon which system software is installed including a firmware interface such as a BIOS and an operating system which supports application software that...
and software
Computer software
Computer software, or just software, is a collection of computer programs and related data that provide the instructions for telling a computer what to do and how to do it....
. This allows a component, whether a piece of hardware such as a graphics card
Video card
A video card, Graphics Card, or Graphics adapter is an expansion card which generates output images to a display. Most video cards offer various functions such as accelerated rendering of 3D scenes and 2D graphics, MPEG-2/MPEG-4 decoding, TV output, or the ability to connect multiple monitors...
or a piece of software such as an Internet browser
Web browser
A web browser is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier and may be a web page, image, video, or other piece of content...
, to function independently while using interfaces to communicate with other components via an input/output
Input/output
In computing, input/output, or I/O, refers to the communication between an information processing system , and the outside world, possibly a human, or another information processing system. Inputs are the signals or data received by the system, and outputs are the signals or data sent from it...
system and an associated protocol.
In addition to hardware and software interfaces, a computing interface may refer to the means of communication between the computer and the user by means of peripheral
Peripheral
A peripheral is a device attached to a host computer, but not part of it, and is more or less dependent on the host. It expands the host's capabilities, but does not form part of the core computer architecture....
devices such as a monitor or a keyboard
Keyboard (computing)
In computing, a keyboard is a typewriter-style keyboard, which uses an arrangement of buttons or keys, to act as mechanical levers or electronic switches...
, an interface with the Internet
Internet
The Internet is a global system of interconnected computer networks that use the standard Internet protocol suite to serve billions of users worldwide...
via Internet Protocol
Internet Protocol
The Internet Protocol is the principal communications protocol used for relaying datagrams across an internetwork using the Internet Protocol Suite...
, and any other point of communication involving a computer.
Hardware Interfaces
Hardware interfaces exist in computing systems between many of the components such as the various buses, storage devices, other I/OI/O
I/O may refer to:* Input/output, a system of communication for information processing systems* Input-output model, an economic model of flow prediction between sectors...
devices, etc. A hardware interface is described by the mechanical, electrical and logical signals at the interface and the protocol for sequencing them (sometimes called signaling). A standard interface, such as SCSI
SCSI
Small Computer System Interface is a set of standards for physically connecting and transferring data between computers and peripheral devices. The SCSI standards define commands, protocols, and electrical and optical interfaces. SCSI is most commonly used for hard disks and tape drives, but it...
, decouples the design and introduction of computing hardware, such as I/O
I/O
I/O may refer to:* Input/output, a system of communication for information processing systems* Input-output model, an economic model of flow prediction between sectors...
devices, from the design and introduction of other components of a computing system, thereby allowing users and manufacturers great flexibility in the implementation of computing systems. Hardware interfaces can be parallel where performance is important or serial
Serial communication
In telecommunication and computer science, serial communication is the process of sending data one bit at a time, sequentially, over a communication channel or computer bus. This is in contrast to parallel communication, where several bits are sent as a whole, on a link with several parallel channels...
where distance is important.
Software Interfaces
A software interface may refer to a range of different types of interface at different "levels": an operating system may interface with pieces of hardware, applicationApplication software
Application software, also known as an application or an "app", is computer software designed to help the user to perform specific tasks. Examples include enterprise software, accounting software, office suites, graphics software and media players. Many application programs deal principally with...
s or program
Computer program
A computer program is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute...
s running on the operating system may need to interact via streams, and in object oriented programs
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...
, objects within an application may need to interact via methods
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
.
Software interfaces in practice
A piece of software provides access to computer resources (such as memory, CPU, storage, etc.) by its underlying computer system; the availability of these resources to other software can have major ramifications—sometimes disastrous ones—for its functionality and stability. A key principle of design is to prohibit access to all resources by default, allowing access only through well-defined entry points, i.e. interfaces.The types of access that interfaces provide between software components can include: constants, data type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
s, types of procedure
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....
s, exception
Exception handling
Exception handling is a programming language construct or computer hardware mechanism designed to handle the occurrence of exceptions, special conditions that change the normal flow of program execution....
specifications and method signatures. In some instances, it may be useful to define public variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...
as part of the interface. It often also specifies the functionality of those procedures and methods, either by comment
Comment (computer programming)
In computer programming, a comment is a programming language construct used to embed programmer-readable annotations in the source code of a computer program. Those annotations are potentially significant to programmers but typically ignorable to compilers and interpreters. Comments are usually...
s or (in some experimental languages) by formal logical assertions and preconditions.
The interface of a software module is deliberately kept separate from the implementation of that module. The latter contains the actual code of the procedures and methods described in the interface, as well as other "private" variables, procedures, etc.. Any other software module (that can be referred to as a client to A) that interacts with is forced to do so only through the interface. One practical advantage of this arrangement is that replacing the implementation of by another one that meets the same specifications of the interface should not cause to fail—as long as its use of complies with the specifications of the interface. (See also Liskov substitution principle
Liskov substitution principle
Substitutability is a principle in object-oriented programming. It states that, in a computer program, if S is a subtype of T, then objects of type T may be replaced with objects of type S without altering any of the desirable properties of that program...
.)
Software interfaces in object oriented languages
In object-oriented languages the term "interface" is often used to define an abstract typeAbstract data type
In computing, an abstract data type is a mathematical model for a certain class of data structures that have similar behavior; or for certain data types of one or more programming languages that have similar semantics...
that contains no data, but exposes behaviors defined as methods
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
. A class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
having all the methods corresponding to that interface is said to implement that interface. Furthermore, a class can implement multiple interfaces, and hence can be of different types at the same time.
An interface is hence a type
Data type
In computer programming, a data type is a classification identifying one of various types of data, such as floating-point, integer, or Boolean, that determines the possible values for that type; the operations that can be done on values of that type; the meaning of the data; and the way values of...
definition; anywhere an object can be exchanged (in a function or method
Method (computer science)
In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...
call) the type of the object to be exchanged can be defined in terms of an interface instead of a specific class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
. This allows later code to use the same function exchanging different object types; hence such code turns out to be more generic and reusable.
Usually a method in an interface cannot be used directly; there must be a class implementing that object to be used for the method invocation. For example, one can define an interface called "Stack
Stack (data structure)
In computer science, a stack is a last in, first out abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, pop and stack top. The push operation adds a new item to the top of the stack,...
" that has two methods:
push
and pop
and later implement it in two different versions, say, FastStack and GenericStack—the first being faster, but working with a stack of fixed size, and the second using a data structure that can be resized, but at the cost of somewhat lower speed.This approach can be pushed to the limit of defining interfaces with a single method; e.g. the Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
language defines the interface Readable that has the single
read
method and a collection of implementations to be used for different purposes, among others: BufferedReader, FileReader, InputStreamReader, PipedReader, and StringReader; or even less, marker interfacesMarker interface pattern
The marker interface pattern is a design pattern in computer science, used with languages that provide run-time type information about objects...
like contain virtually nothing.
In its purest form, an interface (like in Java) must include only method definitions and constant
Constant (programming)
In computer programming, a constant is an identifier whose associated value cannot typically be altered by the program during its execution...
values that make up part of the static interface of a type.
Some languages (like C#) also permit the definition to include properties owned by the object, which are treated as methods with syntactic sugar
Syntactic sugar
Syntactic sugar is a computer science term that refers to syntax within a programming language that is designed to make things easier to read or to express....
, but no constants (in contrast to Java), constructors, destructors, fields, nested types, or operators.
Programming against software interfaces
The use of interfaces allows implementation of a programming style called programming against interfaces. The idea behind this is to base the logic one develops on the sole interface definition of the objects one uses and not to make the code depend on the internal details. This allows the programmer the ability to later change the behavior of the system by simply swapping the object used with another implementing the same interface.Pushing this idea to the limit one can introduce the inversion of control
Inversion of Control
In software engineering, Inversion of Control is an abstract principle describing an aspect of some software architecture designs in which the flow of control of a system is inverted in comparison to procedural programming....
that means leaving the context to inject the code with the specific implementations of the interface that will be used to perform the work.
See also
- Abstraction inversionAbstraction inversionIn computer programming, abstraction inversion is an anti-pattern arising when users of a construct need functions implemented within it but not exposed by its interface...
- Application binary interfaceApplication binary interfaceIn computer software, an application binary interface describes the low-level interface between an application program and the operating system or another application.- Description :...
- Application programming interfaceApplication programming interfaceAn application programming interface is a source code based specification intended to be used as an interface by software components to communicate with each other...
- Computer busComputer busIn computer architecture, a bus is a subsystem that transfers data between components inside a computer, or between computers.Early computer buses were literally parallel electrical wires with multiple connections, but the term is now used for any physical arrangement that provides the same...
- Implementation (computer science)
- Implementation inheritance
- Inheritance semantics
- Modular programmingModular programmingModular programming is a software design technique that increases the extent to which software is composed of separate, interchangeable components called modules by breaking down program functions into modules, each of which accomplishes one function and contains everything necessary to accomplish...
- Software componentry
- User interfaceUser interfaceThe user interface, in the industrial design field of human–machine interaction, is the space where interaction between humans and machines occurs. The goal of interaction between a human and a machine at the user interface is effective operation and control of the machine, and feedback from the...
- Virtual inheritanceVirtual inheritanceVirtual inheritance is a topic of object-oriented programming. It is a kind of inheritance in which the part of the object that belongs to the virtual base class becomes common direct base for the derived class and any next class that derives from it...