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]

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 to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared via compareTo()).

Examples

edit

Some programming languages provide explicit language support for interfaces: Ada, C#, D, Dart, Delphi, Go, Java, Logtalk, Object Pascal, Objective-C, OCaml, PHP, Racket, 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:

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.[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 same things as are interfaces.[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:

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.

See also

edit

Notes

edit
  1. ^ Use 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

edit
  1. ^ a b "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.

📚 Artikel Terkait di Wikipedia

Inheritance (object-oriented programming)

In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object (prototype-based inheritance) or class (class-based

Forwarding (object-oriented programming)

In object-oriented programming, forwarding means that using a member of an object (either a property or a method) results in actually using the corresponding

Index of object-oriented programming articles

encapsulation in object-oriented programming Programming paradigm protected, a way of encapsulation in object-oriented programming Protocol Prototype pattern

Comparison of programming languages (object-oriented programming)

This comparison of programming languages compares how object-oriented programming languages such as C++, Java, Smalltalk, Object Pascal, Perl, Python

Object REXX

Object REXX is a high-level, general-purpose, interpreted, object-oriented (class-based) programming language. Today it is generally referred to as ooRexx

Common Lisp Object System

The Common Lisp Object System (CLOS) is the facility for object-oriented programming in ANSI Common Lisp. CLOS is a dynamic object system which differs

Concept (generic programming)

valid if (*i).m is. Protocol (object-oriented programming) Concepts (C++) Interface (Java) Type class Austern, M.H. Generic programming and the STL: using

Protocol

behavior Protocol (science), a predefined written procedural method of conducting experiments Medical protocol (disambiguation) Protocol (object-oriented programming)