Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Interface (object-oriented programming)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Abstraction of a class}} In [[object-oriented programming]], an '''interface''' or '''protocol''' type{{efn|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.}} 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.<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> <!-- rewrite todo: probably move this to an examples section later --> If objects are fully [[encapsulation (object-oriented programming)|encapsulated]] then the interface is the only way in which they may be accessed by other objects. For example, in [[Java (programming language)|Java]], the <code>Comparable</code> interface specifies a method <code>compareTo()</code> 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 <code>compareTo()</code>). 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 (programming language)|Delphi]], [[Go (programming language)|Go]], [[Java (programming language)|Java]], [[Logtalk]], [[Object Pascal]], [[Objective-C]], [[OCaml]], [[PHP]], [[Racket (programming language)|Racket]], [[Seed7]], [[Swift (programming language)|Swift]], [[Python (programming language)|Python]] 3.8. In languages supporting [[multiple inheritance]], such as [[C++]], interfaces are implemented as [[abstract class]]es. 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 <code>__iter__</code> method and be used as a [[Collection (abstract data type)|collection]].<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> [[Type class]]es in languages like [[Haskell (programming language)|Haskell]], or module signatures in [[ML (programming language)|ML]] and [[OCaml]], are used for many of the things that interfaces are used for.{{clarify|date=November 2022}} In [[Rust (programming language)|Rust]], interfaces are called ''trait''s.<ref>{{cite web|title=Traits - The Rust Reference|date=January 2024|url=https://doc.rust-lang.org/reference/items/traits.html}}</ref> ==See also== * [[Concept (generic programming)]] * [[Delegation (programming)]] * [[Objective-C#Protocols|Protocols in Objective-C]] * [[Class (computer science)]] * [[Encapsulation (computer science)]] * [[Public interface]] * [[Interface (Java)]] * [[Application programming interface]] * [[Interface (computing)]] * [[List of basic computer science topics]] ==Notes== {{notelist}} ==References== {{reflist}} {{Data types}} {{DEFAULTSORT:Protocol (Object-priented programming)}} {{compu-prog-stub}} [[Category:Object-oriented programming]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Clarify
(
edit
)
Template:Compu-prog-stub
(
edit
)
Template:Data types
(
edit
)
Template:Efn
(
edit
)
Template:Notelist
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)