Interface (Java): Difference between revisions

Content deleted Content added
m Add inline citation.
Remove duplicate references, use {{rp}} instead.
Line 1:
{{Short description|Concept in the Java computer programming language}}
{{Merge to|Interface (object-oriented programming)|discuss=Talk:Interface (object-oriented programming)#Proposed merge of Interface (Java) into Protocol (object-oriented programming)|date=November 2022}}
An '''interface''' in the [[Java (programming language)|Java programming language]] is an [[abstract type]] that is used to declare a behavior that [[class (computer science)|classes]] must implement. They are similar to [[Protocol (object-oriented programming)|protocol]]s. Interfaces are declared using the <code>interface</code> [[Java keywords|keyword]], and may only contain [[method signature]] and constant declarations (variable declarations that are declared to be both <code>[[Static_variable#Static_Variables_as_Class_Variables|static]]</code> and <code>[[Final (Java)|final]]</code>). All methods of an Interface do not contain implementation (method bodies) as of all versions below Java 8. Starting with Java 8, <code>default</code><ref name=Bloch>''{{cite book | title= "Effective Java: Programming Language Guide''," third|last=Bloch| first=Joshua| publisher=Addison-Wesley | edition:=third {{ISBN| isbn=978-0134685991}},| year=2018, p.99}}</ref>{{rp|99}} and <code>static</code><ref name=Bloch>''{{cite book | title= "Effective Java: Programming Language Guide''," third|last=Bloch| first=Joshua| publisher=Addison-Wesley | edition:=third {{ISBN| isbn=978-0134685991}},| year=2018, p.7}}</ref>{{rp|7}} methods may have implementation in the <code>interface</code> definition.<ref>{{cite web|url=http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html|title=Default Methods|access-date=2014-06-30|archive-url=https://web.archive.org/web/20170523042436/http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html|archive-date=2017-05-23|url-status=dead}}</ref> Then, in Java 9, <code>private</code> and <code>private static</code> methods were added. At present, a Java interface can have up to six different types.
 
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.
Line 25:
* declares only method headers and public constants.
* cannot be instantiated.
* can be implemented by a class.<ref name=Bloch>''{{cite book | title= "Effective Java: Programming Language Guide''," third|last=Bloch| first=Joshua| publisher=Addison-Wesley | edition:=third {{ISBN| isbn=978-0134685991}},| year=2018, p.75}}</ref>{{rp|75}}
* cannot extend a class.
* can extend several other interfaces.<ref name=Bloch>''{{cite book | title= "Effective Java: Programming Language Guide''," third|last=Bloch| first=Joshua| publisher=Addison-Wesley | edition:=third {{ISBN| isbn=978-0134685991}},| year=2018, p.87}}</ref>{{rp|87}}
 
==Usage==