{{Short description|Abstraction of a class}} In object-oriented programming, an '''interface''' or '''protocol''' type{{efn|Use of these terms varies by programming language. Java and languages derived from it tend to use ''interface'', while ''protocol'' is generally more popular elsewhere.}} 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.<ref name="csharp-learn">{{cite web |title=Interfaces - define behavior for multiple types |url=https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/types/interfaces |website=learn.microsoft.com |access-date=16 November 2022 |language=en-us}}</ref> A class which provides the methods listed in an interface is said to ''implement'' the interface,<ref name="csharp-learn"/> or to ''adopt'' the protocol.<ref name="swift-24h">{{cite book |last1=Miller |first1=BJ |title=Sams Teach Yourself Swift in 24 hours |date=2015 |location=Indianapolis, Indiana |isbn=978-0-672-33724-6 |page=263 |quote=Any type can '''adopt''' a protocol to help give it extra functionality to accomplish a particular set of tasks.}}</ref>
Interfaces are useful for encapsulation and reducing coupling. For example, in Java, the <code>Comparable</code> interface specifies the method <code>compareTo</code>. Thus, a sorting method only needs to take objects of types which implement <code>Comparable</code> to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared via <code>compareTo()</code>).
== Examples == Some programming languages provide explicit language support for interfaces: Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, Swift, Python 3.8. In languages supporting multiple inheritance, such as C++, interfaces are abstract classes.
In Java, an implementation of interfaces may look like:
<syntaxhighlight lang="java"> class Animal { ... } class Theropod extends Animal { ... }
interface Flyable { void fly(); }
interface Vocal { void vocalize(); }
public class Bird extends Theropod implements Flyable, Vocal { // ... public void fly() { ... } public void vocalize() { ... } } </syntaxhighlight>
In languages without explicit support, interfaces are often still present as conventions; this is known as duck typing. For example, in Python, any class can implement an <code>__iter__</code> method and be used as an iterable.<ref name="python-iter">{{cite web |title=Glossary — Python 3.11.0 documentation |url=https://docs.python.org/3/glossary.html#term-iterable |website=docs.python.org |access-date=16 November 2022}}</ref> Classes may also explicitly subclass an ABC, such as {{Code|collections.abc.Iterable}}.
Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the same things as are interfaces.{{clarify|date=November 2022}}
In Rust, interfaces are called ''traits''.<ref>{{cite web|title=Traits - The Rust Reference|date=January 2024|url=https://doc.rust-lang.org/reference/items/traits.html}}</ref> In Rust, a <code>struct</code> does not contain methods, but may add methods through separate {{Code|impl}} blocks:
<syntaxhighlight lang="rust"> trait Pet { fn speak(&self); }
struct Dog { // Structs only contain their fields name: String }
impl Dog { // Not from a trait fn new(name: String) -> Self { Dog { name } } }
impl Pet for Dog { // From a trait fn speak(&self) { println!("{} says 'Woof!'", self.name); } }
fn main() { let dog = Dog::new(String::from("Arlo")); dog.speak(); } </syntaxhighlight>
In C++, there are multiple ways to resemble interfaces. One such way is the Java-style interface, which is done using abstract classes. The other, which resembles Go interfaces, is using concepts. Unlike inheritance, with concepts any type may satisfy a concept (not just classes) so long as it satisfies all of its requirements.
== See also == * Interface (computing) * Protocols in Objective-C * Interface (Java) * Concept (generic programming) * Delegation (programming) * Class (computer science) * Application programming interface
== Notes == {{Notelist}}
== References == {{Reflist}}
{{Data types}}
{{DEFAULTSORT:Protocol (Object-oriented programming)}}
Category:Object-oriented programming Category:Data types Category:Programming language comparisons <!-- Hidden categories below --> Category:Articles with example Java code Category:Articles with example Rust code