Protocol (object-oriented programming)

An interface (English interface) is used in object-oriented programming agreeing common signatures of methods that can be implemented in different classes. The interface is taken, what methods are available or must be present.

Technical details

An interface specifies which methods are available and must be present. In addition to this syntactic definition of a so-called contract should always be defined over which the importance (in terms of preconditions and postconditions ) of the various methods is set - ie its semantics. The contract is usually defined only informally in the Documentation or an external specification of the interface, but are also formal specification languages ​​such as OCL available. Some programming languages ​​such as Eiffel also offer direct syntactic ways of establishing a contract.

Interfaces represent a guarantee with respect to the existing methods in a class. Specify that all objects that hold this interface can be treated equally.

In some programming languages ​​that do not support multiple inheritance ( such as Java ) interfaces can be used to define compatibilities between classes that do not inherit from each other: the interface relationships are not bound by the strict class tree. These interface declarations are often explicitly marked as such ( for example with the interface keyword ). As a replacement for multiple inheritance to interfaces, however, are not suitable because they only define methods and their parameters and do not allow inheritance of functionality.

Declaration

Other languages ​​( mostly those that support multiple inheritance like C ) does not understand the concept of interfaces, but treat them like ordinary classes. We also speak of abstract classes. Sometimes even their own language (called interface description language, IDL) is used to declare the interface - mostly in the middleware systems such as CORBA or DCOM is the case. Object-based languages ​​without strong typing generally have no interfaces.

Definition of constants

In some programming languages ​​such as Java or PHP, it is possible to declare constants inside an interface definition. All implementing classes then are these constants are available.

Classification of interfaces

Interfaces can be classified along two independent criteria: generality and usefulness. Regarding the public interest is drawn between general and context-specific interfaces on the utility between offering and enabling interfaces.

  • General interfaces contain the entire public interface of the callee. They are used to separate the used interface specification of its implementation.
  • Context-specific interfaces do not cover the entire public interface of a class, but only specific aspects of this class. They allow you to use objects of a class in specific roles. For example, a buffer may be used for reading or writing. For each of these access " aspects " can exist a separate interface.
  • Offering interfaces are then, if the caller turns over the interface to the callee. This is the typical and most frequent case in the use of the interface.
  • Enables end interfaces are, conversely, if the callee or even a third component is the real beneficiary of the interface. For example, an object ( or its class) implement the interface Printable. Such an object can then be passed to a printer for printout. Obviously renders the object that satisfies the interface here is not the service, but allows them only.

A special case are so-called marker interfaces that do not require methods to be evaluated through introspection mechanisms at runtime.

Example of an interface

Example, there is an interface account with the method withdraw (), all classes that implement this interface, debit have a method. Another example: A number of classes called savings account, checking account and investment account implement the interface account. The interface has to provide a method getKontostand, ie, all classes with the interface, the getKontostand method.

If in a class multiple interfaces implemented and have their methods, they shall be the same name and same signature ( parameter type and return type ) of access to and implemented in a class with a qualified name, ie by prefixing the package name ( Java) or namespace (. NET). An example in Java:

Public interface Account {      getKontostand int (); / / Abstract signature definition } public class Account implements savings account {      private int account stood;      / / Implementation of the method defined by Account      public int getKontostand ()      {          return account stood;      }      ... } Java differs in a further point against. NET languages ​​is that a class that implements an interface does not have to be explicitly declared. The following example defines a so-called anonymous inner class within a method.

Public account erstelleKonto () {      Account k = new account ()      {          public int getKontostand ()          {              return 0;          }      };      return k; } naming Conventions

In some programming languages ​​, it is common to make interfaces identified by special prefixes or suffixes. Thus, an "I " is often ( for interface ) prepended or appended an "IF" or " interface ". This has no technical reasons but was chosen as a means of better readability and thus maintainability. The above example interface account then that would mean IKonto, account interface or KontoIF.

  • Interfaces are recognized as such by the name.
  • Implementing classes can have a simpler name.
  • Interfaces should not be recognized as such by the name, because you as a user of other objects whose only interface ( ie public methods ) should always bear in mind.
  • Interfaces can be considered as the essential element of the programming. Therefore, it makes more sense to supplement the names of the implementations with prefixes or suffixes.
  • Interfaces are particularly useful when there is more than one implementation, so that the implementing classes are already named with prefixes and suffixes.
716155
de