In
object-oriented programming, an interface or protocol type is a
data type
In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Most progra ...
describing a set of
method signature
In computer science, a type signature or type annotation defines the inputs and outputs for a function, subroutine or method. A type signature includes the number, types, and order of the arguments contained by a function. A type signature is typ ...
s, the implementations of which may be provided by multiple
classes that are otherwise not necessarily related to each other.
A class which provides the methods listed in a protocol is said to ''adopt'' the protocol,
or to ''implement'' the interface.
If objects are fully
encapsulated then the protocol is the only way in which they may be accessed by other objects. For example, in
Java, the
Comparable
interface specifies a method
compareTo()
which implementing classes must implement. This means that a sorting method, for example, can sort a collection of any objects of types which implement the Comparable interface, without having to know anything about the inner nature of the class (except that two of these objects can be compared by means of
compareTo()
).
Some
programming languages provide explicit language support for protocols (
Ada,
C#,
D,
Dart
Dart or DART may refer to:
* Dart, the equipment in the game of darts
Arts, entertainment and media
* Dart (comics), an Image Comics superhero
* Dart, a character from ''G.I. Joe''
* Dart, a ''Thomas & Friends'' railway engine character
* Dar ...
,
Delphi
Delphi (; ), in legend previously called Pytho (Πυθώ), in ancient times was a sacred precinct that served as the seat of Pythia, the major oracle who was consulted about important decisions throughout the ancient classical world. The oracle ...
,
Go,
Java,
Logtalk,
Object Pascal,
Objective-C,
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
,
PHP,
Racket
Racket may refer to:
* Racket (crime), a systematised element of organized crime
** Protection racket, a scheme whereby a group provides protection to businesses or other groups through violence outside the sanction of the law
* Racket (sports equ ...
,
Seed7
Seed7 is an extensible general-purpose programming language designed by Thomas Mertes. It is syntactically similar to Pascal and Ada. Along with many other features, it provides an extension mechanism. Daniel Zingaro"Modern Extensible Languages" ...
,
Swift). In languages supporting
multiple inheritance, such as
C++, interfaces are implemented as
abstract classes.
In languages without explicit support, protocols are often still present as conventions. This is known as
duck typing. For example, in
Python, any class can implement an
__iter__
method and be used as a
collection
Collection or Collections may refer to:
* Cash collection, the function of an accounts receivable department
* Collection (church), money donated by the congregation during a church service
* Collection agency, agency to collect cash
* Collectio ...
.
Type class
In computer science, a type class is a type system construct that supports ad hoc polymorphism. This is achieved by adding constraints to type variables in parametrically polymorphic types. Such a constraint typically involves a type class T and ...
es in languages like
Haskell, or module signatures in
ML and
OCaml
OCaml ( , formerly Objective Caml) is a general-purpose programming language, general-purpose, multi-paradigm programming language which extends the Caml dialect of ML (programming language), ML with object-oriented programming, object-oriented ...
, are used for many of the things that protocols are used for.
See also
*
Concept (generic programming)
*
Delegation (programming)
*
Protocols in Objective-C
*
Class (computer science)
*
Encapsulation (computer science)
*
Public interface
*
List of basic computer science topics
*
Application programming interface
An application programming interface (API) is a way for two or more computer programs to communicate with each other. It is a type of software interface, offering a service to other pieces of software. A document or standard that describes how t ...
Notes
References
{{compu-prog-stub
Object-oriented programming