Interfaces are used to encode similarities which the classes of various types share, but do not necessarily constitute a class relationship. For instance, a human and a parrot can both whistle; however, it would not make sense to represent Humans and Parrots as subclasses of a Whistler class. Rather they most likely be subclasses of an Animal class (likely with intermediate classes), but both would implement the Whistler interface.
Another use of interfaces is being able to use an 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 whistler.whistle() will call the implemented method whistle of object whistler no matter what class it has, provided it implements Whistler. In a more practical example, a sorting algorithm may expect an object of type Comparable. Thus, without knowing the specific type, it knows that objects of that type can somehow be sorted.
For example:
An interface:
Interfaces are defined with the following syntax (compare to Java's class definition):
Example: public interface Interface1 extends Interface2;
The body of the interface contains abstract methods, but since all methods in an interface are, by definition, abstract, the abstract keyword is not required. Since the interface specifies a set of exposed behaviors, all methods are implicitly public.
Thus, a simple interface may be
The member type declarations in an interface are implicitly static, final and public, but otherwise they can be any type of class or interface.6
The syntax for implementing an interface uses this formula:
Classes may implement an interface. For example:
If a class implements an interface and does not implement all its methods, it must be marked as abstract. If a class is abstract, one of its subclasses is expected to implement its unimplemented methods, though if any of the abstract class' subclasses do not implement all interface methods, the subclass itself must be marked again as abstract.
Classes can implement multiple interfaces:
Interfaces can share common class methods:
However a given class cannot implement the same or a similar interface multiple times:
Interfaces are commonly used in the Java language for callbacks,7 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.
Interfaces can extend several other interfaces, using the same formula as described below. For example,
is legal and defines a subinterface. It allows multiple inheritance, unlike classes. Predator and Venomous may possibly define or inherit methods with the same signature, say kill(Prey p). When a class implements VenomousPredator it will implement both methods simultaneously.
Some common Java interfaces are:
Bloch 2018. - Bloch, Joshua (2018). "Effective Java: Programming Language Guide" (third ed.). Addison-Wesley. ISBN 978-0134685991. ↩
"Default Methods". Archived from the original on 2017-05-23. Retrieved 2014-06-30. https://web.archive.org/web/20170523042436/http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html ↩
"The Java Language Specification". http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.5 ↩
Mitchell, John D. (June 1, 1996). "Java Tip 10: Implement callback routines in Java". JavaWorld. Retrieved 2020-07-14. https://www.infoworld.com/article/2077462/java-tip-10--implement-callback-routines-in-java.html ↩