Java interface #
A Java interface is a programming interface with additional (object-oriented) features.
A Java interface is similar to a class, with the exception that the methods that it declares are (by default) not implemented.
Instead, the interface only specifies the behavior of these methods. Their implementations is found in the classes that implement the interface.
Illustration #
Example. We may declare an interface for chess pieces, as follows.
public interface Piece { ... /* Returns `true` iff the piece at coordinates (x1,y1) can move to coordinates (x2,y2) */ bool isValidMove(Piece[][] board, int x1, int y1, int x2, int y2); }Observe that the method
isValidMoveis not implemented (it is only specified).
A class can implement an interface with the keyword
implements.
Example (continued). The classes
QueenandKnightbelow implement the interfacePiece, each with its own implementation of the methodisValidMove.public class Queen implements Piece { ... bool isValidMove(Piece[][] board, int x1, int y1, int x2, int y2){ <some code here> } }$\qquad$
public class Knight implements Piece { ... bool isValidMove(Piece[][] board, int x1, int y1, int x2, int y2){ <some code here> } }
Benefits #
Documentation #
A Java interface lets us expose what is strictly needed to interact with our code (abstracting from implementation details).
Collaboration #
A Java interface acts as a contract, ensuring interoperability. Two programmers can work independently, as long as they respect the interfaces that they agreed upon.
Multiple implementations #
An interface leaves room for alternative implementations of a same method within the same codebase.
For instance:
- operating system or hardware-specific optimization techniques,
- implementations with different guarantees on performances (like the multiple implementations of Java’s Collection interface and subinterfaces).
Syntax #
Inheritance #
Syntax. A Java interface can extend another with the keyword
extends.The syntax is the same as for two classes.
Example.
public interface I extends J { ... }
In contrast to classes, Java interfaces support multiple inheritance: an interface $I$ can extend two interfaces $J$ and $K$, even if $J$ and $K$ do not extend each other.
Example.
public interface I extends J, K { ... }
Implementing an interface #
Syntax. The keyword
implementsdeclares that a class implements an interface (as we have seen already).
Example.
public class C implements I { ... }
Syntax. A class can implement several interface that do not extend each other.
Example.
public class C implements I, J { ... }
Constraints. Consider a class $C$ that implements an interface $I$ :
- if $C$ is abstract, then it can implement some of the methods declared in $I$,
- if $C$ is not abstract, then each method declared in $I$ must be implemented in $C$ (or some superclass of $C$), otherwise the program will not compile.
The program represented by the following diagram does not compile. Can you see why?
The program represented by the following diagram does not compile. Can you see why?
Interface vs abstract class #
In theory, Java’s interfaces and abstract classes serve different purposes:
- abstract classes are meant to factorize code (i.e. avoid redundant code), whereas
- interfaces are meant to document code and act as a contract.
To go further. In practice, interfaces and abstract classes have partially overlapping features (which can be confusing).
In particular:
- an abstract class can have abstract methods, which behave similarly to interface methods (with the additional constraint that an abstract method must be implemented by at least one subclass).
- Since Java 8 (2014), interfaces can carry code, in so-called default methods. This feature was introduced for backward compatibility reasons (and arguably contradicts the meaning of the term “interface”).
Note. Some authors (like Joshua Bloch) recommend using interfaces with default methods in place of abstract classes, because they allow multiple inheritance.
For this course, we chose to present interfaces from a more traditional (pre-Java 8) perspective, because this is (arguably) easier to understand, and still the dominant usage of Java interfaces.