A Java interface defines a contract: it names operations that implementing classes agree to provide, while each class decides how those operations work. Because a variable can use an interface as its type, code can depend on a capability rather than one specific class.
What an interface means in Java
An interface is a reference type, but it cannot be instantiated directly. Think of it as an agreed set of operations a caller may rely on. A class that implements the interface supplies the required implementations. The interface describes method shape and intended interaction; it does not by itself guarantee that every implementation behaves correctly.
As an Amazon Associate I earn from qualifying purchases.
For example, separate manufacturers could build different robotic cars while agreeing that each provides the operations expected by a guidance system. The system can work with the shared contract rather than depend on one manufacturer’s implementation. Oracle’s introductory tutorial uses this kind of separation to explain interfaces: Oracle Java Tutorials: Interfaces.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallHow to declare and implement an interface
This compact example defines a capability, implements it in a class, and then refers to the object through the interface type:
interface Payable {
int amountInCents();
}
final class Invoice implements Payable {
private final int cents;
Invoice(int cents) {
this.cents = cents;
}
@Override
public int amountInCents() {
return cents;
}
}
Payable item = new Invoice(1250);
Payabledeclares the operation callers can use.Invoiceusesimplementsand provides the method body.itemhas the interface type, so calling code can use the contract without depending on the concrete class.
The example illustrates the contract-and-implementation pattern; it is not a claim that the code was compiled or tested.
What kinds of methods can an interface have?
The rules below describe modern Java, with method semantics grounded in the Java SE 21 Language Specification. The older Oracle tutorial remains useful for fundamentals, but its pages identify their examples as JDK 8-era material.
Rank #2
| Method kind | Body? | How it is used | What implementing classes do |
|---|---|---|---|
| Abstract | No | Declared as an operation in the contract. | A concrete implementing class supplies the implementation. |
| Default | Yes | An instance method, called on an implementing object. | Inherits the default unless it overrides it. |
| Static | Yes | Called through the interface itself, not through an instance. | Not inherited as an instance method. |
| Private | Yes | Helper implementation used within the interface. | Not inherited or overridden by implementing classes. |
Under the Java SE 21 rules, a method declared without private, default, or static is implicitly abstract; interface methods in these categories are public unless declared private. Declared interface fields are implicitly public static final. See Java SE 21 JLS §9.4 for method rules and §9.3 for fields.
Why default methods matter
A default method lets an interface provide shared instance behavior, which can help evolve an interface without requiring every existing implementation to add the same method body. An implementing class may override the default. An interface that extends another can inherit a default, redeclare it as abstract, or provide a replacement default. Oracle’s tutorial illustrates these inheritance choices in its default methods explanation.
What is a functional interface?
A functional interface has exactly one abstract method. That single abstract operation gives a lambda expression, method reference, or constructor reference a target type. The interface may still have other methods, such as default or static methods, without changing the one-abstract-method condition.
Runnable task = () -> System.out.println("work");
Here, the lambda supplies the behavior for Runnable‘s single abstract method. This is a language illustration, not an observed test result. The @FunctionalInterface annotation documents intent and asks the compiler to check that the type meets the functional-interface rules; it is optional when the type already meets them. See the Java SE 25 FunctionalInterface API.
Rank #4
When to choose an interface instead of an abstract class
Use an interface when the important design idea is a capability or contract that different classes may implement, including classes that already extend different base classes. A Java class has only one direct superclass, but it can implement multiple interfaces; an interface can also extend multiple interfaces. An abstract class is a class in the inheritance hierarchy, so it can provide shared state and implementation while consuming the class’s one direct-superclass position.
Recommended Free Tools
Neither choice is universally better. Choose based on whether the relationship is best expressed as an implementable capability or as a shared class hierarchy with common state and behavior. Oracle’s tutorial covers interface declarations and inheritance.
Best Value
Which Java references are current?
Oracle’s Java Tutorials are approachable for core concepts, but the resource page states that their examples are based on JDK 8. Use them with that context, and consult the relevant language specification or API documentation for modern rules. The Java SE 21 JLS citations above establish the method categories described here; the functional-interface link is to Java SE 25 API documentation. Oracle’s tutorial resource page was last updated 2024-10-25 and identifies the book edition as The Java Tutorial, Sixth Edition: Oracle Java Tutorials.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




