Learn Java
import com.learnjava.oopadvanced.Interfaces;

Interfaces in Java

@Concept

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.

@HowItWorks
1

Declaring an interface

interface Flyable { void fly(); } declares a method signature with no body, just a contract.

2

Implementing an interface

class Bird implements Flyable requires Bird to supply a real, concrete body for fly().

3

All abstract methods must be implemented

Or the implementing class itself must be declared abstract instead.

4

A class can implement several interfaces at once

Separated by commas after implements, unlike the single-superclass limit on extends.

5

Interfaces hold no instance state

Traditionally just method signatures and constants, though default and static methods added actual code since Java 8.

@Example
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();
}
}
Output
Flapping wings and soaring
1Flyable declares fly() with no body at all, just a signature — a contract, not an implementation.
2Bird implements Flyable, taking on the obligation to supply a real body for fly().
3Bird's version of fly() is a genuine, complete implementation with an actual method body.
4b.fly() calls that implementation directly, printing the single line of output.
@UseCases
Defining a shared contract that unrelated classes can each implement in their own way
Letting a class gain the capabilities of several unrelated behaviors at once, unlike single class inheritance
Writing code that accepts any object implementing a specific interface, regardless of its actual class
Designing an API around what a class can do rather than what it specifically is

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.