Java Interface Default, Static and Private Methods
Since Java 8 and 9, interfaces can contain code. Learn static factory methods in interfaces, private helper methods, default methods and why they enable interface evolution, source vs binary compatibility, and the rules for resolving default method conflicts with X.super.method().
kvi5hnuCode Sprout team member10 min readOn this page
Originally, Java interfaces could contain only abstract methods and constants. That changed in Java 8, which added static and default methods, and in Java 9, which added private methods. Today an interface can contain real code, while still having no instance fields. This article explains each kind of method, why it was added, and what happens when default methods collide.
| Kind of interface method | Since | Has a body? | Implicit modifiers | Purpose |
|---|---|---|---|---|
| abstract | 1.0 | No | public abstract | The contract implementors must fulfil |
static | 8 | Yes | public (unless private) | Factory and utility methods |
default | 8 | Yes | public | Ready-made behavior implementors may override |
private / private static | 9 | Yes | none | Helpers shared by the other methods |
Static Methods
As of Java 8, you can add static methods to interfaces. There was never a technical reason to outlaw them; it just seemed against the spirit of interfaces as abstract specifications.
Before Java 8, static helpers lived in companion classes. That's why the standard library has pairs such as Collection/Collections and Path/Paths. Today the Path interface has the factory methods itself:
public interface Path {
static Path of(URI uri) { . . . }
static Path of(String first, String... more) { . . . }
. . .
}
The old Paths.get(...) methods still exist, but the documentation recommends Path.of. Likewise, List.of, Set.of, Map.of, Comparator.comparing and Stream.of are static methods of interfaces. There's no longer a reason to write a separate utility class for an interface you design.
Static interface methods are called with the interface name (Path.of(...), Shape.unitSquare()). Unlike static methods of classes, they are not inherited by implementing classes or subinterfaces, so Circle.unitSquare() doesn't compile.
Private Methods
Since Java 9, methods in an interface can be private, either static or instance methods. Private methods can only be used by the other methods of the interface itself, so their use is limited to being helper methods. They let several default or static methods share code without making that code part of the public API. In the demo below, Shape.format is a private static helper used by the default method describe.
Default Methods
Put the keyword default in front of an interface method and give it a body. Every implementing class then gets that body unless it overrides it:
public interface Greeter {
String name();
default String greet() { return "Hello, " + name() + "!"; } // used unless overridden
}
A class implementing Greeter only has to supply name. Two situations make defaults especially handy.
Defaults for optional operations
Some interfaces list operations that not every implementation can support. java.util.Iterator, covered in the collections part, is the classic case. Its remove method has a default body that simply refuses:
public interface Iterator<E> {
boolean hasNext();
E next();
default void remove() { throw new UnsupportedOperationException("remove"); }
. . .
}
hasNext and next stay abstract, because only you know how to walk your data. A read-only iterator simply leaves remove alone and inherits the refusal, as Countdown does in the demo below.
Defaults built on abstract methods
A default body may call any other method of the interface, abstract ones included. That lets you derive convenience methods from a small core:
public interface SizedCollection {
int size(); // implementors supply this
default boolean isEmpty() { return size() == 0; } // derived for free
}
An implementor writes size once and gets isEmpty without extra work.
Interface evolution
The most important reason for default methods is interface evolution. Consider the Collection interface, which has been part of Java for decades. Suppose you long ago wrote
public class Bag implements Collection
Java 8 added a stream method to Collection. Suppose stream weren't a default method:
- Source compatibility breaks.
Bagno longer compiles, because it doesn't implement the new method. Adding a non-default method to an interface is not source-compatible. - Binary compatibility survives, sort of. If you don't recompile and use an old JAR containing
Bag, the class still loads. Programs can constructBaginstances, and nothing bad happens. (Adding a method to an interface is binary compatible.) But if a program callsstreamon aBag, anAbstractMethodErroroccurs.
Making stream a default method solves both problems. Bag compiles again, and if the old class is loaded without recompiling, calling stream on a Bag runs Collection.stream. That's how Java 8 added stream, forEach, removeIf and spliterator to the collection interfaces, and how Java 21 added getFirst, getLast and reversed through the new SequencedCollection interface, without breaking millions of existing classes.
Everything in One Program
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
public class Solution {
public static void main(String[] args) {
// static factory methods in an interface
Path p = Path.of("home", "user", "notes.txt");
System.out.println(p.getFileName() + " " + p.getNameCount());
System.out.println(Shape.unitSquare().describe());
// default methods calling abstract methods
Shape c = new Circle(1);
System.out.println(c.describe());
System.out.println(c.isLargerThan(Shape.unitSquare()));
// a read-only iterator gets remove() for free
Iterator<Integer> it = new Countdown(3);
while (it.hasNext()) System.out.print(it.next() + " ");
System.out.println();
try {
it.remove();
} catch (UnsupportedOperationException e) {
System.out.println("remove: " + e.getMessage());
}
// interface evolution: Bag compiled before size-based defaults still works
Bag bag = new Bag(List.of("x", "y"));
System.out.println(bag.size() + " " + bag.isEmpty());
// conflict resolution
System.out.println(new SmartPhone().status()); // interfaces clash: resolved explicitly
System.out.println(new Tablet().status()); // class wins
}
}
interface Shape {
double area(); // abstract: implementors must supply it
default String describe() { // default: may call other interface methods
return name() + " with area " + format(area());
}
default boolean isLargerThan(Shape other) {
return area() > other.area();
}
default String name() {
return getClass().getSimpleName();
}
static Shape unitSquare() { // static factory method
return new Square(1);
}
private static String format(double value) { // private helper, invisible to implementors
return String.format(java.util.Locale.US, "%.2f", value);
}
}
record Square(double side) implements Shape {
public double area() { return side * side; }
}
record Circle(double radius) implements Shape {
public double area() { return Math.PI * radius * radius; }
}
class Countdown implements Iterator<Integer> {
private int current;
Countdown(int start) { current = start; }
public boolean hasNext() { return current > 0; }
public Integer next() {
if (current <= 0) throw new NoSuchElementException();
return current--;
}
// no remove(): the default throws UnsupportedOperationException
}
interface SizedCollection {
int size();
default boolean isEmpty() { return size() == 0; }
}
class Bag implements SizedCollection {
private final List<String> items;
Bag(List<String> items) { this.items = items; }
public int size() { return items.size(); }
}
interface Camera {
default String status() { return "lens ready"; }
}
interface Phone {
default String status() { return "signal: 4 bars"; }
}
class SmartPhone implements Camera, Phone {
@Override
public String status() { // required: both interfaces supply status
return Phone.super.status() + ", " + Camera.super.status();
}
}
class Device {
public String status() { return "battery 80%"; }
}
class Tablet extends Device implements Phone { } // Device.status wins over Phone.status
notes.txt 3
Square with area 1.00
Circle with area 3.14
true
3 2 1
remove: remove
2 false
signal: 4 bars, lens ready
battery 80%
Resolving Default Method Conflicts
A class can end up with two candidate bodies for the same method: one from a default in an interface, and another from a superclass or a second interface. Java settles this with just two rules, far fewer than the multiple-inheritance rules of C++:
- The class hierarchy wins. If a superclass (direct or further up) declares a concrete method with the same name and parameter types, that method is used and every interface default for it is ignored.
- Competing interfaces force a decision. If two interfaces both offer the method and at least one of them has a default body, the compiler refuses to pick. The implementing class must override the method itself.

Two interfaces, one method
Picture two capability interfaces that each report a status line:
interface Camera {
default String status() { return "lens ready"; }
}
interface Phone {
default String status() { return "signal: 4 bars"; }
}
class SmartPhone implements Camera, Phone { } // does not compile
SmartPhone would inherit two unrelated bodies, so javac stops with types Camera and Phone are incompatible; class SmartPhone inherits unrelated defaults for status() from types Camera and Phone. The fix is to write status in SmartPhone. Inside the override you can still reach either default with InterfaceName.super.method(), or combine both:
class SmartPhone implements Camera, Phone {
@Override
public String status() { return Phone.super.status() + ", " + Camera.super.status(); }
}
The rule doesn't relax when only one side has a body. Change Phone so that status is abstract:
interface Phone {
String status();
}
You might expect SmartPhone to quietly take Camera's default. It doesn't: the compiler still demands an override (SmartPhone is not abstract and does not override abstract method status() in Phone). The language treats every overlap the same way, so you never have to remember which flavour of conflict you're in.
Only when neither interface supplies a body is there nothing to resolve. The class implements the method once, or is declared abstract.
The superclass beats the interface
Now let the method arrive from a class and an interface at the same time:
class Device {
public String status() { return "battery 80%"; }
}
class Tablet extends Device implements Phone { } // compiles; status() comes from Device
Tablet uses Device.status. Whether Phone.status is a default or abstract makes no difference, since the inherited concrete method satisfies the interface. In the demo, Tablet prints Device's text, not Phone's.
This "class wins" rule protects existing code: when a library later adds a default to an interface, any class that already had a method with that signature keeps its own behaviour.
Calling a superinterface default
X.super.m() only works when X is a direct superinterface of the type you're writing. A subinterface can also refine an inherited default, or take the body away again so that implementors must write one:
interface QuietPhone extends Phone {
default String status() { return Phone.super.status() + " (muted)"; }
}
interface StrictPhone extends Phone {
String status(); // re-abstracted: implementors of StrictPhone must define status
}
Common questions
Why can't a default method override toString or equals?
Because of the "class wins" rule. Every class inherits toString and equals from Object, so an interface default could never take effect. The compiler forbids such defaults to avoid the confusion. You can still declare them as abstract methods in an interface to document a stronger contract.
Can I call a default method from the implementing class's override?
Yes, with InterfaceName.super.method(), as long as InterfaceName is a direct superinterface of the class. This is useful when you want to extend the default behaviour rather than replace it.
Are static interface methods inherited?
No. You must call them through the interface name: Comparator.naturalOrder(), not through an implementing class or an instance. This avoids ambiguity when a class implements several interfaces with static methods of the same name.
When should I add a default method to an existing interface? When the new method can be implemented correctly for every existing implementation in terms of the other interface methods. If some implementations need different behaviour (thread safety, performance), document that they should override it. Adding a default can still clash with a method that an implementation already has with a different return type.
What's the difference between an abstract class and an interface with defaults? Interfaces still have no instance state and no constructors, and a class can implement many of them. Abstract classes can hold fields, have constructors and enforce access control on members. Use interfaces for types and capabilities, and abstract classes when shared state is needed.
Summary
- Static interface methods (Java 8) hold factories and utilities, replacing companion classes like
Paths. Call them with the interface name; they aren't inherited. - Private interface methods (Java 9) are helpers for the interface's own methods.
- Default methods (Java 8) supply implementations that implementors may override. They can call abstract methods and are ideal for optional operations and convenience methods.
- Defaults make interface evolution possible. Adding an abstract method breaks source compatibility, and calling it on an old class gives
AbstractMethodError. - Conflicts: class wins over interface defaults; when two interfaces clash and at least one has an implementation, the class must override and may pick one with
Interface.super.method(). - Default methods can't override
Objectmethods such astoStringandequals.
Practice Questions
Why did Java 8 introduce default methods instead of just adding abstract methods like stream to Collection?
Adding abstract methods would have broken every existing class implementing Collection, at compile time, and with AbstractMethodError at runtime for old binaries. Default methods let the interface grow while existing implementations keep working.
interface A { default void hello() {...} } and interface B { default void hello() {...} }. Class C implements A, B and wants A's behavior. Write C.
class C implements A, B {
@Override
public void hello() { A.super.hello(); }
}
class Base { public String id() { return "base"; } } interface Tagged { default String id() { return "tag"; } } class D extends Base implements Tagged {}. What does new D().id() return?
"base". The superclass method wins, and the interface default is ignored.
Can you declare default boolean equals(Object o) in an interface? Why or why not?
No. It's a compile-time error, because by the "class wins" rule Object.equals would always take precedence, so such a default could never be called.
What's the difference between a static method in an interface and a static method in a class with respect to inheritance?
A static method of a class can be called through a subclass name. A static interface method is not inherited by implementing classes or subinterfaces; you must call it with the interface name.
When would you use a private interface method?
When two or more default or static methods share logic, such as validation or formatting, that shouldn't become part of the interface's public API.
Previous: Java Interfaces: Declaring and Implementing Interfaces
Related reading
- Java Interfaces: Declaring and Implementing Interfaces
- Java Inheritance Design Hints: Best Practices for Class Hierarchies
- Java Reflection: Invoking Methods and Writing Generic Array Code
- Java Reflection: Analyzing Classes and Inspecting Objects at Runtime
- Java Reflection: The Class Class, Exceptions Primer and Loading Resources

More in Core Java I
Java volatile, Atomics, Deadlocks and ThreadLocal: Safe Sharing Between Threads
Share data between Java threads without bugs: memory visibility and volatile, final fields, AtomicInteger and compareAndSet, LongAdder, lazy holder initialization, safe publication, lock-ordering deadlocks, ThreadLocal, InheritableThreadLocal and ScopedValue.
Java Condition Objects and the synchronized Keyword: await, signalAll, wait and notify
Make threads wait for a condition safely with Condition.await and signalAll, then learn Java's built-in alternative: synchronized methods and blocks, intrinsic locks, wait and notifyAll, choosing lock objects, client-side locking, and the monitor concept.
Java Race Conditions and ReentrantLock: Synchronizing Shared Data
Why unsynchronized threads corrupt shared data, shown step by step with a lost-update race, and how to fix it with ReentrantLock: the lock/try/finally idiom, critical sections, reentrancy and hold counts, tryLock, fair locks, and the mistakes that cause bugs.
Java invokeAll, invokeAny and Fork-Join: Running Groups of Tasks
Coordinate many concurrent tasks in Java: invokeAll to wait for every result, invokeAny to take the first success, timeouts, ExecutorCompletionService for results in completion order, structured concurrency, and the fork-join framework with RecursiveTask, RecursiveAction and work stealing.
Discussion
Sign in to ask a question or share what you learned.