<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Interface_%28object-oriented_programming%29</id>
	<title>Interface (object-oriented programming) - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Interface_%28object-oriented_programming%29"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Interface_(object-oriented_programming)&amp;action=history"/>
	<updated>2026-06-18T09:01:02Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Interface_(object-oriented_programming)&amp;diff=122160&amp;oldid=prev</id>
		<title>24.50.56.74: /* Examples */</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Interface_(object-oriented_programming)&amp;diff=122160&amp;oldid=prev"/>
		<updated>2025-10-15T20:06:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Examples&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Abstraction of a class}}&lt;br /&gt;
In [[object-oriented programming]], an &amp;#039;&amp;#039;&amp;#039;interface&amp;#039;&amp;#039;&amp;#039; or &amp;#039;&amp;#039;&amp;#039;protocol&amp;#039;&amp;#039;&amp;#039; type{{efn|Use of these terms varies by programming language. Java and languages derived from it tend to use &amp;#039;&amp;#039;interface&amp;#039;&amp;#039;, while &amp;#039;&amp;#039;protocol&amp;#039;&amp;#039; is generally more popular elsewhere.}} is a [[data type]] that acts as an [[abstraction]] of a [[Class (computer science)|class]]. It describes a set of [[method signature]]s, the implementations of which may be provided by multiple [[class (computer programming)|class]]es that are otherwise not necessarily related to each other.&amp;lt;ref name=&amp;quot;csharp-learn&amp;quot;&amp;gt;{{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}}&amp;lt;/ref&amp;gt; A class which provides the methods listed in an interface is said to &amp;#039;&amp;#039;implement&amp;#039;&amp;#039; the interface,&amp;lt;ref name=&amp;quot;csharp-learn&amp;quot;/&amp;gt; or to &amp;#039;&amp;#039;adopt&amp;#039;&amp;#039; the protocol.&amp;lt;ref name=&amp;quot;swift-24h&amp;quot;&amp;gt;{{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 &amp;#039;&amp;#039;&amp;#039;adopt&amp;#039;&amp;#039;&amp;#039; a protocol to help give it extra functionality to accomplish a particular set of tasks.}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Interfaces are useful for [[Encapsulation (computer programming)|encapsulation]] and reducing [[Coupling (computer programming)|coupling]]. For example, in [[Java (programming language)|Java]], the &amp;lt;code&amp;gt;Comparable&amp;lt;/code&amp;gt; interface specifies the method &amp;lt;code&amp;gt;compareTo&amp;lt;/code&amp;gt;. Thus, a sorting method only needs to take objects of types which implement &amp;lt;code&amp;gt;Comparable&amp;lt;/code&amp;gt; to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared via &amp;lt;code&amp;gt;compareTo()&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
Some [[programming language]]s provide explicit language support for interfaces: [[Ada (programming language)|Ada]], [[C Sharp (programming language)|C#]], [[D (programming language)|D]], [[Dart (programming language)|Dart]], [[Delphi (software)|Delphi]], [[Go (programming language)|Go]], [[Java (programming language)|Java]], [[Logtalk]], [[Object Pascal]], [[Objective-C]], [[OCaml]], [[PHP]], [[Racket (programming language)|Racket]], [[Swift (programming language)|Swift]], [[Python (programming language)|Python]] 3.8. In languages supporting [[multiple inheritance]], such as [[C++]], interfaces are [[abstract class]]es.&lt;br /&gt;
&lt;br /&gt;
In Java, an implementation of interfaces may look like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class Animal { ... }&lt;br /&gt;
class Theropod extends Animal { ... }&lt;br /&gt;
&lt;br /&gt;
interface Flyable {&lt;br /&gt;
    void fly();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
interface Vocal {&lt;br /&gt;
    void vocalize();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Bird extends Theropod implements Flyable, Vocal {&lt;br /&gt;
    // ...&lt;br /&gt;
    public void fly() { ... }&lt;br /&gt;
    public void vocalize() { ... }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In languages without explicit support, interfaces are often still present as conventions; this is known as [[duck typing]]. For example, in [[Python (programming language)|Python]], any class can implement an &amp;lt;code&amp;gt;__iter__&amp;lt;/code&amp;gt; method and be used as an [[Iterator|iterable]].&amp;lt;ref name=&amp;quot;python-iter&amp;quot;&amp;gt;{{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}}&amp;lt;/ref&amp;gt; Classes may also explicitly subclass an [[Abstract base class|ABC]], such as {{Code|collections.abc.Iterable}}.&lt;br /&gt;
&lt;br /&gt;
[[Type class]]es in languages like [[Haskell]], or module signatures in [[ML (programming language)|ML]] and [[OCaml]], are used for many of the same things as are interfaces.{{clarify|date=November 2022}}&lt;br /&gt;
&lt;br /&gt;
In [[Rust (programming language)|Rust]], interfaces are called &amp;#039;&amp;#039;traits&amp;#039;&amp;#039;.&amp;lt;ref&amp;gt;{{cite web|title=Traits - The Rust Reference|date=January 2024|url=https://doc.rust-lang.org/reference/items/traits.html}}&amp;lt;/ref&amp;gt; In Rust, a &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; does not contain methods, but may add methods through separate {{Code|impl}} blocks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
trait Pet {&lt;br /&gt;
    fn speak(&amp;amp;self);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
struct Dog {&lt;br /&gt;
    // Structs only contain their fields&lt;br /&gt;
    name: String&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
impl Dog {&lt;br /&gt;
    // Not from a trait&lt;br /&gt;
    fn new(name: String) -&amp;gt; Self {&lt;br /&gt;
        Dog { name }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
impl Pet for Dog {&lt;br /&gt;
    // From a trait&lt;br /&gt;
    fn speak(&amp;amp;self) {&lt;br /&gt;
        println!(&amp;quot;{} says &amp;#039;Woof!&amp;#039;&amp;quot;, self.name);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fn main() {&lt;br /&gt;
    let dog = Dog::new(String::from(&amp;quot;Arlo&amp;quot;));&lt;br /&gt;
    dog.speak();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Interface (computing)]]&lt;br /&gt;
* [[Objective-C#Protocols|Protocols in Objective-C]]&lt;br /&gt;
* [[Interface (Java)]]&lt;br /&gt;
* [[Concept (generic programming)]]&lt;br /&gt;
* [[Delegation (programming)]]&lt;br /&gt;
* [[Class (computer science)]]&lt;br /&gt;
* [[Application programming interface]]&lt;br /&gt;
&lt;br /&gt;
== Notes ==&lt;br /&gt;
{{Notelist}}&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
&lt;br /&gt;
{{Data types}}&lt;br /&gt;
&lt;br /&gt;
{{DEFAULTSORT:Protocol (Object-oriented programming)}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Object-oriented programming]]&lt;br /&gt;
[[Category:Data types]]&lt;br /&gt;
[[Category:Programming language comparisons]]&lt;br /&gt;
&amp;lt;!-- Hidden categories below --&amp;gt;&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Articles with example Rust code]]&lt;/div&gt;</summary>
		<author><name>24.50.56.74</name></author>
	</entry>
</feed>