encyclopegia.org

Interface (object-oriented programming)

In object-oriented programming, an interface or protocol type 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. A class which provides the methods listed in an interface is said to implement the interface, or to adopt the protocol. Interfaces are useful for encapsulation and reducing coupling. For example, in Java, the java.lang.Comparable interface specifies the method compareTo() . Thus, a sorting method only needs to take objects of types which implement java.lang.Comparable to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared via compareTo() ).

In object-oriented programming, an interface or protocol type 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. A class which provides the methods listed in an interface is said to implement the interface, or to adopt the protocol. Interfaces are useful for encapsulation and reducing coupling. For example, in Java, the java.lang.Comparable interface specifies the method compareTo() . Thus, a sorting method only needs to take objects of types which implement java.lang.Comparable to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared via compareTo() ). class Animal { ... } class Theropod extends Animal { ... } interface Flyable { void fly(); } interface Vocal { void vocalize(); } public class Bird extends Theropod implements Flyable, Vocal { // ... public void fly() { ... } public void vocalize() { ... } } 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. Classes may also explicitly subclass an ABC, such as . Type classes in languages like Haskell, or module signatures in ML and OCaml, are used for many of the same things as are interfaces. In Rust, interfaces are called traits. In Rust, a struct does not contain methods, but may add methods through separate blocks: trait Pet { fn speak(&self); } struct Dog { // Structs only contain their fields name: String } impl Dog { // Not from a trait fn new(name: String) -> Self { Dog { name } } } impl Pet for Dog { // From a trait fn speak(&self) { println!("{} says 'Woof!'", self.name); } } fn main() { let dog = Dog::new(String::from("Arlo")); dog.speak(); } In C++, there are multiple ways to resemble interfaces. One such way is the Java-style interface, which is done using abstract classes. The other, which resembles Go interfaces, is using concepts. Unlike inheritance, with concepts any type may satisfy a concept (not just classes) so long as it satisfies all of its requirements.

References

  1. Interfaces - define behavior for multiple types . learn.microsoft.com . retrieved 16 Novemb · retrieved 2026-06-22 · rev 1360723777
    Cite this reference

    MLA "Interfaces - define behavior for multiple types . learn.microsoft.com . retrieved 16 Novemb." Interfaces - define behavior for multiple types. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777. Accessed 24 November 2026.

    Chicago "Interfaces - define behavior for multiple types . learn.microsoft.com . retrieved 16 Novemb." Interfaces - define behavior for multiple types. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777.

    APA Interfaces - define behavior for multiple types. (2026). Interfaces - define behavior for multiple types . learn.microsoft.com . retrieved 16 Novemb. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1360723777

  2. Glossary — Python 3.11.0 documentation . docs.python.org . retrieved 16 November 2022 · retrieved 2026-06-22 · rev 1360723777
    Cite this reference

    MLA "Glossary — Python 3.11.0 documentation . docs.python.org . retrieved 16 November 2022." Glossary — Python 3. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777. Accessed 24 November 2026.

    Chicago "Glossary — Python 3.11.0 documentation . docs.python.org . retrieved 16 November 2022." Glossary — Python 3. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777.

    APA Glossary — Python 3. (2026). Glossary — Python 3.11.0 documentation . docs.python.org . retrieved 16 November 2022. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1360723777

  3. Traits - The Rust Reference . January 2024. · retrieved 2026-06-22 · rev 1360723777
    Cite this reference

    MLA "Traits - The Rust Reference . January 2024." Traits - The Rust Reference. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777. Accessed 24 November 2026.

    Chicago "Traits - The Rust Reference . January 2024." Traits - The Rust Reference. 2026-06-22. https://en.wikipedia.org/w/index.php?oldid=1360723777.

    APA Traits - The Rust Reference. (2026). Traits - The Rust Reference . January 2024. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1360723777

Cite this article

MLA

"Interface (object-oriented programming)." encyclopegia.org, 2026, https://encyclopegia.org/wiki/interface-object-oriented-programming. Accessed 24 November 2026.

Chicago

"Interface (object-oriented programming)." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/interface-object-oriented-programming.

APA

encyclopegia.org. (2026). Interface (object-oriented programming). Retrieved November 24, 2026, from https://encyclopegia.org/wiki/interface-object-oriented-programming

Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON

Related articles

Indirection

enwiki · references available

Object (computer science)

enwiki · references available
CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1360723777 · 0.5 · snapshot enwiki-001360723777

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.