Interfaces in Java
An interface declares a set of method signatures with no implementation, traditionally, defining a contract that any implementing class must fulfill by providing a real body for each one. A class implements an interface with the implements keyword, and must supply concrete implementations of every abstract method the interface declares, or itself be declared abstract, deferring that obligation.
Unlike a class, a Java class can implement any number of interfaces at once, which is how Java gets some of the flexibility of multiple inheritance without the ambiguity problems that inheriting from multiple classes at once would create. An interface only ever provides a contract to fulfill, plus, since Java 8, some default behavior, never state of its own to conflict with another interface's.
Declaring an interface
interface Flyable { void fly(); } declares a method signature with no body, just a contract.
Implementing an interface
class Bird implements Flyable requires Bird to supply a real, concrete body for fly().
All abstract methods must be implemented
Or the implementing class itself must be declared abstract instead.
A class can implement several interfaces at once
Separated by commas after implements, unlike the single-superclass limit on extends.
Interfaces hold no instance state
Traditionally just method signatures and constants, though default and static methods added actual code since Java 8.
interface Flyable { void fly();} public class Bird implements Flyable { public void fly() { System.out.println("Flapping wings and soaring"); } public static void main(String[] args) { Bird b = new Bird(); b.fly(); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.