Protocol (object-oriented programming)

Last updated

Protocol is a term used by particular object-oriented programming languages with a variety of specific meanings, which other languages may term interface or trait .


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 discrete objects to communicate with each other. These are definitions of methods and values which the objects agree upon, in order to co-operate, as 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 represented 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 are 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)[ citation needed ]. 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

Related Research Articles

In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state and implementations of behavior. In many languages, the class name is used as the name for the class, the name for the default constructor of the class, and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.

Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.

In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space, which is coded as if it were a normal (local) procedure call, without the programmer explicitly coding the details for the remote interaction. That is, the programmer writes essentially the same code whether the subroutine is local to the executing program, or remote. This is a form of client–server interaction, typically implemented via a request–response message-passing system. In the object-oriented programming paradigm, RPCs are represented by remote method invocation (RMI). The RPC model implies a level of location transparency, namely that calling procedures are largely the same whether they are local or remote, but usually they are not identical, so local calls can be distinguished from remote calls. Remote calls are usually orders of magnitude slower and less reliable than local calls, so distinguishing them is important.

In computing, serialization or serialisation is the process of translating a data structure or object state into a format that can be stored or transmitted and reconstructed later. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward. Serialization of object-oriented objects does not include any of their associated methods with which they were previously linked.

The Common Object Request Broker Architecture (CORBA) is a standard defined by the Object Management Group (OMG) designed to facilitate the communication of systems that are deployed on diverse platforms. CORBA enables collaboration between systems on different operating systems, programming languages, and computing hardware. CORBA uses an object-oriented model although the systems that use the CORBA do not have to be object-oriented. CORBA is an example of the distributed object paradigm.

In object-oriented programming, the iterator pattern is a design pattern in which an iterator is used to traverse a container and access the container's elements. The iterator pattern decouples algorithms from containers; in some cases, algorithms are necessarily container-specific and thus cannot be decoupled.

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.

A method in object-oriented programming (OOP) is a procedure associated with a message and an object. An object consists of data and behavior; these comprise an interface, which specifies how the object may be utilized by any of its various consumers.

In computer science, reflection programming is the ability of a process to examine, introspect, and modify its own structure and behavior.

In computer science, dynamic dispatch is the process of selecting which implementation of a polymorphic operation to call at run time. It is commonly employed in, and considered a prime characteristic of, object-oriented programming (OOP) languages and systems.

Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.

In computer science, message passing is a technique for invoking behavior on a computer. The invoking program sends a message to a process and relies on that process and its supporting infrastructure to then select and run some appropriate code. Message passing differs from conventional programming where a process, subroutine, or function is directly invoked by name. Message passing is key to some models of concurrency and object-oriented programming.

Distributed object

In distributed computing, distributed objects are objects that are distributed across different address spaces, either in different processes on the same computer, or even in multiple computers connected via a network, but which work together by sharing data and invoking methods. This often involves location transparency, where remote objects appear the same as local objects. The main method of distributed object communication is with remote method invocation, generally by message-passing: one object sends a message to another object in a remote machine or process to perform some task. The results are sent back to the calling object.

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones such as super class or base class and then forming them into a hierarchy of classes. In most class-based object-oriented languages, an object created through inheritance, a "child object", acquires all the properties and behaviors of the "parent object", with the exception of: constructors, destructor, overloaded operators and friend functions of the base class. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors, to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed graph.

Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem. It is one approach to software design.

Logtalk is an object-oriented logic programming language that extends and leverages the Prolog language with a feature set suitable for programming in the large. It provides support for encapsulation and data hiding, separation of concerns and enhanced code reuse. Logtalk uses standard Prolog syntax with the addition of a few operators and directives.

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python, and others manipulate data structures.

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code: data in the form of fields, and code, in the form of procedures.

The composition filters model denotes a modular extension to the conventional object model. It provides a solution for a wide range of problems in the construction of large and complex applications. Most notably, one implementation of composition filters provides an abstraction layer for message-passing systems.


  1. "The Objective-C programming Language:Protocols". Apple Inc. Retrieved 7 October 2012.
  2. Official Python 3 Documentation, Abstract Object Layer