interface_object-oriented_programming

Interface (object-oriented programming)

Return to Programming terms

May also be referred to as Protocol (object-oriented programming).

Interface is a term used by particular object-oriented programming languages with a variety of specific meanings, which other languages may term protocol or trait (or even Dynamic dispatch or Dependency injection).

(Protocol when used otherwise is akin to a Communication protocol, indicating the chain of interactions between the caller and the object.)

Languages which use the term Protocol include:

In these languages, a protocol is a common means for unrelated objects to communicate with each other. These are definitions of methods and values which the objects agree upon in order to co-operate, part of an API.

The protocol/interface is a description of:

  1. The messages that are understood by the object.
  2. The arguments that these messages may be supplied with.
  3. The types of results that these messages return.
  4. The invariants that are preserved despite modifications to the state of an object.
  5. The exceptional situations that will be required to be handled by clients to the object.
  6. (For the communications-style usage only:) The call sequence and decision points of the methods, such as would be representing in UML interaction diagrams: Communication diagram, Sequence diagram, Interaction overview diagram/Activity diagram, Timing diagram.

If the objects are fully encapsulated then the protocol will describe the only way in which objects may be accessed by other objects. For example, in Java interfaces, the Comparable interface specifies a method compareTo() which implementing classes must implement. This means that a separate sorting method, for example, can sort any object which implements 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/interfaces (Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, PHP, Racket, Seed7, Swift). In C++ interfaces are known as abstract base classes and implemented using pure virtual functions. The object-oriented features in Perl also support interfaces.

Non-object oriented languages

Although the Go programming language is not generally considered an object-oriented language, it does allow methods to be defined on user-defined types. Go has “interface” types that are compatible with any type that supports a given set of methods (the type does not need to explicitly implement the interface). The empty interface,

interface{}

, is compatible with all types.

Note that functional programming and distributed programming languages use the term protocol akin to the communications usage (i.e. a specification of allowed exchanges of messages, emphasis on exchanges, not on messages)

. In particular, the following are also considered as part of a protocol in these languages:

  1. The allowed sequences of messages,
  2. Restrictions placed on either participant in the communication,
  3. Expected effects that will occur as the message is handled.

Type classes in languages like Haskell are used for many of the things that protocols are used for.

See also

References

External sites

Categories

interface_object-oriented_programming.txt · Last modified: 2020/11/20 01:10 by 127.0.0.1