Interface (object-oriented programming)

Revision as of 14:07, 24 May 2025 by 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.))
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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.<ref name="csharp-learn">{{#invoke:citation/CS1|citation |CitationClass=web }}</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">Template:Cite book</ref>

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.<ref name="python-iter">{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

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.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>

See alsoEdit

NotesEdit

Template:Notelist

ReferencesEdit

Template:Reflist Template:Data types


Template:Compu-prog-stub