Interface (Java): Difference between revisions

Content deleted Content added
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 56:
... implements ''InterfaceName''[, ''another interface'', ''another'', ...] ...
 
[[Class (software)|Classes]] may implement an interface. For example, :
<syntaxhighlight lang="java">
public class Lion implements Predator {
Line 62:
@Override
public boolean chasePrey(Prey p) {
// Programming to chase prey p (specifically for a lion)
}
 
Line 91:
</syntaxhighlight>
 
Interfaces are commonly used in the Java language for [[Callback (computer science)|callbacks]],<ref>{{cite web |last1=Mitchell |first1=John D. |date=1996-06-01 |df=mdy |url=httphttps://www.javaworldinfoworld.com/javaworldarticle/javatips2077462/jwjava-javatip10tip-10--implement-callback-routines-in-java.html |title=Java WorldTip 10: Implement callback routines in Java |work=[[JavaWorld]] |accessdate=2020-07-14}}</ref> as Java does not allow multiple inheritance of classes, nor does it allow the passing of methods (procedures) as arguments. Therefore, in order to pass a method as a parameter to a target method, current practice is to define and pass a reference to an interface as a means of supplying the signature and address of the parameter method to the target method rather than defining multiple variants of the target method to accommodate each possible calling class.
 
===Subinterfaces===