Content deleted Content added
No edit summary Tag: Reverted |
m Dating maintenance tags: {{When}} |
||
(19 intermediate revisions by 17 users not shown) | |||
Line 1:
{{Short description|Concept in the Java computer programming language}}
An '''interface''' in the [[Java (programming language)|Java programming language]] is an [[abstract type]] that is used to
Interfaces cannot be [[Instance (computer science)|instantiated]], but rather are implemented. A class that implements an interface must implement all of the non-default methods described in the interface, or be an [[abstract class]]. Object references in Java may be specified to be of an interface type; in each case, they must either be [[null pointer|null]], or be bound to an object that implements the interface.
One benefit of using interfaces is that they simulate [[multiple inheritance]]. All classes in Java must have exactly one [[base class]], the only exception being {{Javadoc:SE|package=java.lang|java/lang|Object}} (the [[top type|root class]] of the Java [[type system]]); [[multiple inheritance]] of classes is not allowed. However, an interface may inherit multiple interfaces and a class may implement multiple interfaces.
Line 9 ⟶ 10:
Another use of interfaces is being able to use an [[Object (computer science)|object]] without knowing its type of class, but rather only that it implements a certain interface. For instance, if one were annoyed by a whistling noise, one may not know whether it is a human or a parrot, because all that could be determined is that a whistler is whistling. The call <code>whistler.whistle()</code> will call the implemented method <code>whistle</code> of object <code>whistler</code> no matter what class it has, provided it implements <code>Whistler</code>. In a more practical example, a [[sorting algorithm]] may expect an object of type {{Javadoc:SE|java/lang|Comparable}}. Thus, without knowing the specific type, it knows that objects of that type can somehow be sorted.
For example:
<syntaxhighlight lang="Java">
Line 23 ⟶ 24:
* declares only method headers and public constants.
* cannot be instantiated.
* can be implemented by a class.{{sfn|Bloch|2018}}{{rp|75}}
* cannot extend a class.
* can extend several other interfaces.{{sfn|Bloch|2018}}{{rp|87}}
==Usage==
Line 35 ⟶ 36:
''constant declarations''
''abstract method declarations''
'' static method declarations''
}
Line 49 ⟶ 51:
</syntaxhighlight>
The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.<ref>{{cite web|url=http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.5|title=The Java Language Specification}}</ref>
===Implementing interfaces in a class===
Line 108 ⟶ 110:
==See also==
* [[Interface (object-oriented programming)]]
* [[Mixin]]
* [[Trait (computer programming)]]
==
{{reflist}}
==References==
*{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}
==External links==
|