Interface (Java): Difference between revisions

Content deleted Content added
clarified the difference between an interface and an abstract class.
Tags: Reverted Mobile edit Mobile web edit
AnomieBOT (talk | contribs)
m Dating maintenance tags: {{When}}
 
(5 intermediate revisions by 5 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 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>{{sfn|Bloch|2018}}{{rp|99}} and <code>static</code>{{sfn|Bloch|2018}}{{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,{{when|date=March 2025}} a Java interface can have up to six different types.{{Clarify|date=March 2025}}
 
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.
 
There can be some confusion about the difference between an interface and an abstract class in Java. The technical difference is that an interface can only declare methods (which are implicitly abstract) and constants. On the other hand, an abstract class can have members, methods (abstract or not), and constructors<ref>Python Abstract Class vs Interface[https://ioflood.com/blog/java-abstract-class-vs-interface/]</ref>.
 
== Overview ==
Line 53 ⟶ 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===