Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Interface (object-oriented programming)

From Wikipedia, the free encyclopedia
(Redirected fromProtocol (object-oriented programming))
Abstraction of a class

Inobject-oriented programming, aninterface orprotocol type[a] is adata type that acts as anabstraction of aclass. It describes a set ofmethod signatures, the implementations of which may be provided by multipleclasses that are otherwise not necessarily related to each other.[1] A class which provides the methods listed in an interface is said toimplement the interface,[1] or toadopt the protocol.[2]

Interfaces are useful forencapsulation and reducingcoupling. For example, inJava, theComparable interface specifies the methodcompareTo. Thus, a sorting method only needs to take objects of types which implementComparable to sort them, without knowing about the inner nature of the class (except that two of these objects can be compared viacompareTo()).

Examples

[edit]

Someprogramming 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 supportingmultiple inheritance, such asC++, interfaces areabstract 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 asduck typing. For example, inPython, any class can implement an__iter__ method and be used as aniterable.[3] Classes may also explicitly subclass anABC, such ascollections.abc.Iterable.

Type classes in languages likeHaskell, or module signatures inML andOCaml, are used for many of the same things as are interfaces.[clarification needed]

InRust, interfaces are calledtraits.[4] In Rust, astruct does not contain methods, but may add methods through separateimpl blocks:

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

See also

[edit]

Notes

[edit]
  1. ^Use of these terms varies by programming language. Java and languages derived from it tend to useinterface, whileprotocol is generally more popular elsewhere.

References

[edit]
  1. ^ab"Interfaces - define behavior for multiple types".learn.microsoft.com. Retrieved16 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 canadopt 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. Retrieved16 November 2022.
  4. ^"Traits - The Rust Reference". January 2024.
Uninterpreted
Numeric
Pointer
Text
Composite
Other
Related
topics
Retrieved from "https://en.wikipedia.org/w/index.php?title=Interface_(object-oriented_programming)&oldid=1317009412"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp