# Interface (object-oriented programming)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Interface_(object-oriented_programming)
> Markdown URL: https://mediated.wiki/source/Interface_(object-oriented_programming).md
> Source: https://en.wikipedia.org/wiki/Interface_(object-oriented_programming)
> Source revision: 1345557148
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

{{Short description|Abstraction of a class}}
In [object-oriented programming](/source/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](/source/data_type) that acts as an [abstraction](/source/Abstraction_(computer_science)) of a [class](/source/Class_(programming)). It describes a set of [method signature](/source/method_signature)s, 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](/source/Encapsulation_(computer_programming)) and reducing [coupling](/source/Coupling_(computer_programming)). For example, in [Java](/source/Java_(programming_language)), 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 language](/source/programming_language)s provide explicit language support for interfaces: [Ada](/source/Ada_(programming_language)), [C#](/source/C_Sharp_(programming_language)), [D](/source/D_(programming_language)), [Dart](/source/Dart_(programming_language)), [Delphi](/source/Delphi_(software)), [Go](/source/Go_(programming_language)), [Java](/source/Java_(programming_language)), [Logtalk](/source/Logtalk), [Object Pascal](/source/Object_Pascal), [Objective-C](/source/Objective-C), [OCaml](/source/OCaml), [PHP](/source/PHP), [Racket](/source/Racket_(programming_language)), [Swift](/source/Swift_(programming_language)), [Python](/source/Python_(programming_language)) 3.8. In languages supporting [multiple inheritance](/source/multiple_inheritance), such as [C++](/source/C%2B%2B), interfaces are [abstract class](/source/abstract_class)es.

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](/source/duck_typing). For example, in [Python](/source/Python_(programming_language)), any class can implement an <code>__iter__</code> method and be used as an [iterable](/source/Iterator).<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](/source/Abstract_base_class), such as {{Code|collections.abc.Iterable}}.

[Type class](/source/Type_class)es in languages like [Haskell](/source/Haskell), or module signatures in [ML](/source/ML_(programming_language)) and [OCaml](/source/OCaml), are used for many of the same things as are interfaces.{{clarify|date=November 2022}}

In [Rust](/source/Rust_(programming_language)), 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](/source/Concepts_(C%2B%2B)). 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)](/source/Interface_(computing))
* [Protocols in Objective-C](/source/Objective-C)
* [Interface (Java)](/source/Interface_(Java))
* [Concept (generic programming)](/source/Concept_(generic_programming))
* [Delegation (programming)](/source/Delegation_(programming))
* [Class (computer science)](/source/Class_(computer_science))
* [Application programming interface](/source/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

---
Adapted from the Wikipedia article [Interface (object-oriented programming)](https://en.wikipedia.org/wiki/Interface_(object-oriented_programming)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Interface_(object-oriented_programming)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
