Interface (object-oriented programming): Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Remsense
Reverted 1 edit by Wukuendo (talk): Like I already told you in edit summary, I see no reason for mention of this little-known language in every possible location where it possibly relevant. It's not WP:DUE, or at least you've made no attempt to demonstrate that it is (i.e. that its mention is of any real relevance to readers, and doesn't amount to advertising.)
 
imported>Villaida
removed stub template
 
Line 2: Line 2:
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>
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 -->
<!-- rewrite todo: probably move this to an examples section later -->== Examples ==
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>).
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>).


Line 34: Line 34:


{{DEFAULTSORT:Protocol (Object-priented programming)}}
{{DEFAULTSORT:Protocol (Object-priented programming)}}
{{compu-prog-stub}}
[[Category:Object-oriented programming]]
[[Category:Object-oriented programming]]
[[Category:Data types]]

Latest revision as of 19:32, 7 June 2025

Template:Short description In object-oriented programming, an interface or protocol typeTemplate:Efn 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 an interface is said to implement the interface,[1] or to adopt the protocol.[2]

Examples

If objects are fully encapsulated then the interface 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 interfaces: 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, interfaces 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 interfaces are used for.Template:Clarify

In Rust, interfaces are called traits.[4]

See also

Notes

Template:Notelist

References

Template:Reflist Template:Data types

  1. a b Script error: No such module "citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "citation/CS1".