DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

Java Interfaces: What They Are and How to Use Them

A Java interface defines a contract that classes can implement. See a concise example, understand abstract, default, static, and private methods, and learn how functional interfaces enable lambdas.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How 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);
  • Payable declares the operation callers can use.
  • Invoice uses implements and provides the method body.
  • item has 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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.