Protocol (object-oriented programming)

From Lojban
Jump to navigation Jump to search

In object-oriented programming, a protocol or interface 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 cooperate.

For example, in Java programming language (where protocols are termed interfaces), the Comparable interface specifies a method compareTo() which implementing classes should 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()).

The protocol 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.