Interface (object-oriented programming)

Last updated

In object-oriented programming, an interface or protocol type [a] 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]

Contents

Interfaces are useful for encapsulation and reducing coupling. For example, in Java, the Comparable interface specifies the method compareTo. Thus, a sorting method only needs to take objects of types which implement Comparable in order to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared by means of compareTo()).

Examples

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 abstract classes.

In Java, an implementation of interfaces may look like:

classAnimal{...}classTheropodextendsAnimal{...}interfaceFlyable{voidfly();}interfaceVocal{voidvocalize();}publicclassBirdextendsTheropodimplementsFlyable,Vocal{// ...publicvoidfly(){...}publicvoidvocalize(){...}}

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 an iterable. [3] Classes may also explicitly subclass an ABC, such as collections.abc.Iterable.

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.[ clarification needed ]

In Rust, interfaces are called traits. [4] In Rust, a struct does not contain methods, but may add methods through separate impl blocks:

traitSpeak{fnspeak(&self);}structDog{// Structs only contain their fieldsname:String}implDog{// Not from a traitfnnew(name:String)->Self{Dog{name}}}implSpeakforDog{// From a traitfnspeak(&self){println!("{} says 'Woof!'",self.name);}}fnmain(){letdog=Dog::new("Arlo".to_string());dog.speak();}

See also

Notes

  1. 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.

References

  1. 1 2 "Interfaces - define behavior for multiple types". learn.microsoft.com. Retrieved 16 November 2022.
  2. Miller, BJ (2015). Sams Teach Yourself Swift in 24 hours. Indianapolis, Indiana. p. 263. ISBN   978-0-672-33724-6. Any type can adopt a protocol to help give it extra functionality to accomplish a particular set of tasks.{{cite book}}: CS1 maint: location missing publisher (link)
  3. "Glossary — Python 3.11.0 documentation". docs.python.org. Retrieved 16 November 2022.
  4. "Traits - The Rust Reference". January 2024.