Interfaces in Java
An interface in Java is a contract: a list of methods a class promises to provide. Code that depends on the contract works with any class that signs it. This is what makes Java programs easy to extend and test. You'll learn how to write and implement interfaces, what default and static methods are, and how lambdas fit in.
What is an interface in Java?
Think of a power socket. Your laptop charger, phone charger, and kettle are completely different devices, but they all fit the same socket shape. The socket doesn't care what's plugged in, only that the plug matches. An interface is the socket shape for code.
public interface Discount {
double apply(double price); // the contract: no body
} A class "signs" the contract with implements and must provide every abstract method:
public class PercentOff implements Discount {
private final double percent;
public PercentOff(double percent) {
this.percent = percent;
}
@Override
public double apply(double price) {
return price * (1 - percent / 100);
}
}
public class FlatOff implements Discount {
private final double amount;
public FlatOff(double amount) {
this.amount = amount;
}
@Override
public double apply(double price) {
return Math.max(0, price - amount);
}
} Why use interfaces?
Because code can depend on the contract instead of a specific class:
static double checkout(double price, Discount discount) {
return discount.apply(price);
}
System.out.println(checkout(80.0, new PercentOff(25))); // 60.0
System.out.println(checkout(80.0, new FlatOff(10))); // 70.0 60.0 70.0
checkout never mentions PercentOff or FlatOff. Tomorrow you can add a
BuyOneGetOneFree discount and checkout keeps working unchanged. That's
polymorphism and loose coupling.
Can a class implement more than one interface?
Yes, as many as it needs: class Invoice implements Printable, Exportable. This is Java's answer to
multiple inheritance, covered in inheritance in Java.
What can go inside a Java interface?
Older tutorials say interfaces contain "only abstract methods". That has been out of date since Java 8 (2014). Modern interfaces can hold five kinds of members:
Explore interface members
abstract method
The contract itself. No body; every implementing class must provide one. Implicitly public abstract.
double apply(double price); default method
Java 8+. Has a body that implementing classes inherit. They may override it. Lets library authors add methods without breaking existing classes.
default String describe() {
return "Discount";
} static method
Java 8+. A helper that belongs to the interface itself. Call it as Discount.none(). It is not inherited by implementing classes.
static Discount none() {
return price -> price;
} private method
Java 9+. A helper used by default or static methods inside the interface, to avoid repeating code. Invisible to everyone else.
private double round(double v) {
return Math.round(v * 100) / 100.0;
} constant
Any field is automatically public static final, a constant that must be initialised and never changes.
double MAX_PERCENT = 90.0; Here's our interface using all of them together:
public interface Discount {
double MAX_PERCENT = 90.0; // constant
double apply(double price); // abstract
default String describe() { // default (Java 8+)
return "Saves " + round(100 - apply(100)) + "%";
}
static Discount none() { // static (Java 8+)
return price -> price;
}
private double round(double v) { // private (Java 9+)
return Math.round(v * 100) / 100.0;
}
} Discount d = new PercentOff(15);
System.out.println(d.describe());
System.out.println(Discount.none().apply(49.99)); Saves 15.0% 49.99
Functional interfaces and lambdas
Our Discount interface has exactly one abstract method. That makes it a
functional interface, and Java lets you implement it with a short lambda
expression instead of a whole class:
Discount blackFriday = price -> price * 0.5;
Discount loyalty = price -> price - 5;
System.out.println(checkout(80.0, blackFriday)); // 40.0
System.out.println(checkout(80.0, loyalty)); // 75.0 price -> price * 0.5— read it as "given a price, return half of it". Java figures out this is the body ofapply.- Add
@FunctionalInterfaceabove the interface so the compiler warns you if someone adds a second abstract method.
Java ships with many ready-made functional interfaces in java.util.function, such as
Function, Predicate, and Supplier. Sorting with a Comparator
lambda is shown in the ArrayList guide.
Interface vs abstract class: which should you use?
| Interface | Abstract class | |
|---|---|---|
| How many can a class use? | Many | One |
| Per-object fields | ✘ No | ✔ Yes |
| Constructors | ✘ No | ✔ Yes |
| Good for | A capability unrelated classes share ("can be compared", "can be saved") | A family of related classes sharing code and state |
Full comparison: abstract class in Java.
Interfaces you'll use every day
List,Set,Map— the Collections Framework is built on interfaces. You writeList<String> names = new ArrayList<>().ComparableandComparator— define how objects are ordered for sorting.Runnable— a task to run, often on another thread.AutoCloseable— lets an object be used in try-with-resources so it's closed automatically.
Quick recap
- An interface is a contract; classes sign it with
implements. - Abstract methods have no body;
default,static, andprivatemethods do. - Fields in an interface are always
public static finalconstants. - An interface with one abstract method is functional, so you can implement it with a lambda.
- A class can implement many interfaces but extend only one class.
What's next?
Now that you know the building blocks, explore the Java keywords
that tie them together, starting with this.
Frequently Asked Questions
What is an interface in Java?
An interface is a contract that lists methods a class promises to provide. Any class that implements the interface must supply those methods. This lets you write code that works with any class following the contract, without caring which exact class it is.
Can a Java interface have method bodies?
Yes, since Java 8. Interfaces can contain default methods (inherited by implementing classes), static methods (called on the interface itself), and since Java 9, private helper methods. Abstract methods still have no body.
Can an interface have variables in Java?
Only constants. Any field you declare in an interface is automatically public, static, and final, so it must be given a value and can never change. Interfaces can't hold per-object state.
How many interfaces can a Java class implement?
As many as you like. This is how Java supports a form of multiple inheritance: a class can extend only one class, but it can implement any number of interfaces, separated by commas.
What is a functional interface?
An interface with exactly one abstract method, such as Runnable or Comparator. Because there's only one method to fill in, you can implement it with a short lambda expression instead of writing a whole class.