Interface (object-oriented programming)

Short description: Abstraction of a class

In object-oriented programming, an interface or protocol type[lower-alpha 1] is a data type that acts as an abstraction of a class. It describes a set of method signatures, the implementations of which may be provided by multiple classes that are otherwise not necessarily related to each other.[1] A class which provides the methods listed in a protocol is said to adopt the protocol,[2] or to implement the interface.[1]

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, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, Seed7, Swift, Python 3.8). 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.[3]

Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the things that protocols are used for.[clarification needed]

In Rust, interfaces are called traits.[4]

## See also

* Concept (generic programming)
* Delegation (programming)
* Protocols in Objective-C
* Class (computer science)
* Encapsulation (computer science)
* Public interface
* Interface (Java)
* List of basic computer science topics
* Application programming interface

## Notes

1. ↑ Usage of these terms varies by programming language. Java and languages derived from it tend to use "interface", while "protocol" is generally more popular elsewhere.

## References

1. ↑ 1.0 1.1 "Interfaces - define behavior for multiple types" (in en-us). https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/interfaces.
2. ↑ Miller, BJ (2015). Sams Teach Yourself Swift in 24 hours. Indianapolis, Indiana. p. 263. ISBN 978-0-672-33724-6. "Any type can adopt a protocol to help give it extra functionality to accomplish a particular set of tasks."
3. ↑ "Glossary — Python 3.11.0 documentation". https://docs.python.org/3/glossary.html#term-iterable.
4. ↑ "Traits - The Rust Reference". January 2024. https://doc.rust-lang.org/reference/items/traits.html.

* v
* t
* e

Data types

Uninterpreted|

* Bit
* Byte
* Trit
* Tryte
* Word
* Bit array

Numeric|

* Arbitrary-precision or bignum
* Complex
* Decimal
* Fixed point
* Floating point
* Double precision
* Extended precision
* Long double
* Octuple precision
* Quadruple precision
* Single precision
* Reduced precision
* Minifloat
* Half precision
* bfloat16
* Integer
* signedness
* Interval
* Rational

Pointer|

* Address
* physical
* virtual
* Reference

Text|

* Character
* String
* null-terminated

Composite|

* Algebraic data type
* generalized
* Array
* Associative array
* Class
* Dependent
* Equality
* Inductive
* Intersection
* List
* Object
* metaobject
* Option type
* Product
* Record or Struct
* Refinement
* Set
* Union
* tagged

Other|

* Boolean
* Bottom type
* Collection
* Enumerated type
* Exception
* Function type
* Opaque data type
* Recursive data type
* Semaphore
* Stream
* Top type
* Type class
* Unit type
* Void

Related
topics|

* Abstract data type
* Data structure
* Generic
* Kind
* metaclass
* Parametric polymorphism
* Primitive data type
* Protocol
* interface
* Subtyping
* Type constructor
* Type conversion
* Type system
* Type theory

See also platform-dependent and independent units of information

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Interface (object-oriented programming). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Interface_(object-oriented_programming)&oldid=2574136"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template