Interface (computer science)

From Wikipedia, the free encyclopedia

An interface defines the communication boundary between two entities, such as a piece of software, a hardware device, or a user. It generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer. Because interfaces are a form of indirection, some additional overhead is incurred versus direct communication.

The interface between a human and a computer is called a user interface. Interfaces between hardware components are physical interfaces. This article deals with software interfaces which exist between separate software components and provide a programmatic mechanism by which these components can communicate.

Contents

[edit] Interfaces in practice

A piece of software is provided 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 (See Information hiding).

The types of access that interfaces provide between software components can include: constants, data types, types of procedures, exception specifications and method signatures. In some instances, it may be useful to define variables as part of the interface. It often also specifies the functionality of those procedures and methods, either by comments or (in some experimental languages) by formal logical assertions.

The interface of a software module A 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 B (which can be referred to as a client to A) that interacts with A is forced to do so only through the interface. One practical advantage of this arrangement is that replacing the implementation of A by another one that meets the same specifications of the interface should not cause B to fail — as long as its use of A complies with the specifications of the interface (See also Liskov substitution principle).

[edit] Uses of interfaces

The concept of interface is the cornerstone of modular programming, a forerunner and a standard ingredient of object-oriented programming. In object-oriented programming, an object's interface consists of a set of methods that the object must respond to. Note that the object does not make its instance variables a part of its interface - these are typically accessed by means of accessor methods.

Some object-oriented programming languages mandate that the interface to the object be specified to the compiler separately from the implementation of that object, whilst others relax the requirement. For example, a class in a programming languages such as Objective-C consists of its interface, specified in a header file, and the implementation in the source file. Because of the dynamically typed nature of Objective-C, one can send messages to any object, and the interface to the class becomes important as it specifies the methods the class responds to.

Interfaces were historically derived from the header files of the C programming language by restricting their syntactic context and contents, and making them a part of the language semantics (as opposed to a mere preprocessor feature).

The Java programming language takes a different approach to the concept of the interface normally existing in other object-oriented programming languages (i.e. that the interface specified is the interface to the class), in that an interface specifies a set of methods which implement some specific functionality, common to a set of classes. See protocol (object-oriented programming).

Some programming languages (e.g. D, Java, Logtalk) allows the definition of interface hierarchies. This allows easy definition of e.g. both minimal and extended versions of an interface.

Some programming languages (e.g. Logtalk) support private and protected implementation of an interface. Thus, the (public) methods declared in an interface can easily become private or protected methods of a class implementing the interface.

The Eiffel language includes in the interface of a class its invariants and the pre- and postconditions of the methods of the class. This is essential to the methodology of design by contract, and may be regarded as an extension of the conditions imposed by the types of the arguments. These rules may be specified in the implementation of a class or in an ancestor which may leave the methods unimplemented. They are extracted by language processors to provide an interface view in the development environment and to generate run-time assertions (checks) in debug versions. The language also ensures that derived classes obey the contracts of their ancestors.

[edit] Supporting Languages

Certain programming languages have different methodologies for allowing the construction of interfaces. In general, any programming language can implement an interface, but the following programming languages provide specific interface constructs of some kind or another:

[edit] See also