Java Interface default, static and private Methods (OCP 21)
Concrete interface methods for the OCP 21 exam: default methods and their six rules, resolving duplicate defaults with Interface.super, the class-wins rule, static interface methods that aren't inherited, private and private static helpers, and who may call what.
kvi5hnuCode Sprout team member8 min readOn this page
Up to Java 7, an interface could hold only abstract methods and constants. Modern Java lets interfaces contain code too. There are four kinds of concrete interface methods, and each has its own rules:
| Kind | Since | Keyword(s) | Implicit modifier | Belongs to |
|---|---|---|---|---|
| default method | Java 8 | default | public | each implementing object |
| static method | Java 8 | static | public | the interface itself |
| private method | Java 9 | private | — | each object (interface-internal) |
| private static method | Java 9 | private static | — | the interface (interface-internal) |
Together with abstract methods and constants, that makes six kinds of interface member.
default methods
A default method has the default keyword and a body. Implementing classes inherit it, and they may override it. The main reason the feature exists is interface evolution: you can add a method to a widely used interface without breaking every class that already implements it. List.sort, Map.getOrDefault and Iterable.forEach were all added this way in Java 8.
public interface Thermostat {
double currentTemp(); // abstract: must be implemented
default double targetTemp() { return 20.0; } // default: optional to override
}
public class HallThermostat implements Thermostat {
public double currentTemp() { return 18.5; } // required
public double targetTemp() { return 21.0; } // optional override
}
The word "default" has three unrelated meanings in Java: a default method, the default label in a switch, and "default access", which is really package access and has no keyword.
The rules
- A default method can be declared only in an interface.
default void a() {}in a class fails with "modifier default not allowed here". - It must have the
defaultkeyword and a body. - It's implicitly
public, and it can't beprotectedorprivate. - It can't be
abstract,finalorstatic. - Implementing classes may override it, following the normal overriding rules. The override must be
public. - If a class inherits two defaults with the same signature from unrelated interfaces, the class must override the method.
public interface Feeder {
public default void refill(); // DOES NOT COMPILE: missing method body, or declare abstract
public int portions() { return 3; } // DOES NOT COMPILE: interface abstract methods cannot have body
final default void lock() { } // DOES NOT COMPILE: modifier final not allowed here
static default void reset() { } // DOES NOT COMPILE: illegal combination of modifiers
}
A default method can't redefine toString(), equals() or hashCode(). javac reports "default method toString in interface X overrides a member of java.lang.Object".
Calling a default method
A default method is an instance method. You call it on an object, never on the interface name:
public interface Melody { default int tempo() { return 120; } }
public class Song implements Melody {
static void play() {
System.out.println(new Song().tempo()); // OK
System.out.println(Melody.tempo()); // DOES NOT COMPILE: non-static method cannot be referenced from a static context
}
}
Conflicting default methods
If two unrelated interfaces supply defaults with the same signature, the compiler won't pick one:
interface Walker { default int speed() { return 5; } }
interface Runner { default int speed() { return 12; } }
class Robot implements Walker, Runner { } // DOES NOT COMPILE:
// types Walker and Runner are incompatible; class Robot inherits unrelated defaults for speed()
The class fixes it by overriding the method. Inside the override, it can still reach either parent's version with the special syntax InterfaceName.super.method():
class Robot implements Walker, Runner {
public int speed() {
return Math.max(Walker.super.speed(), Runner.super.speed()); // 12
}
}
Walker.this.speed() doesn't work ("not an enclosing class"). The syntax is Interface.super.method(), and Interface must be a direct super-interface of the class. You can't skip a level, so A.super.m() fails when you only implement B extends A.
The same rule applies when one interface has a default and the other an abstract method with the same signature ("inherits abstract and default for read()").
This isn't limited to concrete classes. An abstract class, or even an interface that extends both, gets the same compile error. The class or interface resolves it by declaring the method itself, either with a body or, in an abstract class or interface, as a new abstract method:
interface Reader { default String read() { return "r"; } }
interface Parser { String read(); }
abstract class Json implements Reader, Parser { } // DOES NOT COMPILE
abstract class Csv implements Reader, Parser { public abstract String read(); } // OK: conflict resolved
When there's no conflict
Two situations look like conflicts but aren't:
- The class wins. A method inherited from a superclass beats a default from an interface. If
class Basehaspublic String hi(), thenclass Kid extends Base implements GreeterusesBase's version, andGreeter.hi()is ignored. The superclass method must bepublic, though. A package-accesshi()fails because it would "assign weaker access privileges". - The more specific interface wins. If
interface B extends Aand both definedefault m(), then a class implementing both getsB's version with no error, becauseBalready overridesA.
An interface can also re-abstract a default: interface B extends A { String m(); } turns the inherited default back into an abstract method, which implementing classes then have to implement.
static interface methods
A static interface method works like a static method in a class. It has a body, needs no object, and is implicitly public unless it's marked private. Factory methods are the usual use, as in List.of(), Comparator.naturalOrder() and Map.entry().
public interface Jump {
static int maxHeight() { return 8; }
}
The important rule: static interface methods are not inherited. You must call them with the interface name:
public class Frog implements Jump {
void show() {
System.out.println(Jump.maxHeight()); // OK
System.out.println(maxHeight()); // DOES NOT COMPILE: cannot find symbol
System.out.println(Frog.maxHeight()); // DOES NOT COMPILE
System.out.println(this.maxHeight()); // DOES NOT COMPILE
}
}
Even a sub-interface name doesn't work: interface HighJump extends Jump, then HighJump.maxHeight() fails. Because static methods aren't inherited, two interfaces with the same static method can never conflict.
A static interface method also can't be abstract (it needs a body) or final.
private and private static interface methods
Private interface methods exist so that default and static methods can share code without exposing it. They must have a body, and they're visible only inside the interface's own body. Implementing classes can't call them.
public interface Shift {
default void morning() { remind(7); }
default void evening() { remind(19); }
static void overtime() { remind(22); }
private static void remind(int hour) { // shared helper
System.out.println(hour > 18 ? "Late shift" : "Starts at " + hour);
}
}
Which interface members may call which follows the same logic as instance and static methods in a class:

- Instance side (
abstract,defaultandprivatemethods): these run on an object, so they can call everything in the interface. - Static side (
staticandprivate staticmethods): these have no object, so they can only call other static members and read constants. A static method calling a default or a non-static private method fails with "non-static method … cannot be referenced from a static context". - Outside the interface: abstract and default methods are callable through an object, static methods only as
Interface.method(), and private ones not at all.
Try this one:
public interface Tram {
int line();
private static void ring() { }
default void depart() { line(); ring(); } // OK: instance side can call anything
static void brake() { depart(); } // DOES NOT COMPILE: static can't call a default
static void bell() { ring(); } // OK: static to private static
}
What's current
- Nothing in this article changed in Java 22 through 25, so the rules apply to both the Java 21 exam (1Z0-830) and the Java 25 exam (1Z0-831).
privateinterface methods (Java 9) are the newest member type. No interface member kinds were added in Java 10 through 25.
Exam traps from this article
A complete example
The program defines an interface that uses all four kinds of concrete method, a second interface with a clashing default, and a class that resolves the clash by calling both parent versions.
public class Solution {
interface Notifier {
String channel(); // abstract
default String send(String message) { // default: inherited, overridable
return "[" + channel() + "] " + tidy(message);
}
static Notifier sms() { // static: call as Notifier.sms()
return () -> "SMS";
}
private String tidy(String message) { // private instance helper
return shorten(message.strip(), maxLength());
}
default int maxLength() { return 20; }
private static String shorten(String s, int max) { // private static helper
return s.length() <= max ? s : s.substring(0, max - 3) + "...";
}
}
interface Audited {
default String send(String message) { // same signature as Notifier.send
return "audit: " + message.length() + " chars";
}
}
// Inherits two unrelated defaults for send(String): must override
static class EmailNotifier implements Notifier, Audited {
@Override public String channel() { return "EMAIL"; }
@Override public int maxLength() { return 30; } // overrides a default
@Override
public String send(String message) {
String delivered = Notifier.super.send(message); // pick each parent version
String audit = Audited.super.send(message);
return delivered + " | " + audit;
}
}
public static void main(String[] args) {
String text = " Your parcel arrives tomorrow between 9 and 11 ";
Notifier sms = Notifier.sms();
System.out.println(sms.send(text)); // uses default + private helpers
Notifier email = new EmailNotifier();
System.out.println(email.send(text));
Audited asAudit = new EmailNotifier();
System.out.println(asAudit.send("hi")); // same object type, same override
}
}
Output:
[SMS] Your parcel arriv...
[EMAIL] Your parcel arrives tomorro... | audit: 51 chars
[EMAIL] hi | audit: 2 chars
Points to notice:
Notifier.sms()is a static factory. Its body uses a lambda to implement the one abstract methodchannel(). Lambdas come in chapter 8, and for now read() -> "SMS"as "aNotifierwhosechannel()returns"SMS"".send()(default) callstidy()(private), which callsshorten()(private static) andmaxLength()(default). The instance side may call both sides.EmailNotifierinherits two unrelatedsend(String)defaults, so it must override the method. It combines both versions withNotifier.super.sendandAudited.super.send.- Overriding the default
maxLength()changes how the inheritedsend()behaves for email, because default methods are polymorphic.
Common questions
Why can't default methods be final?
Their whole purpose is to provide a replaceable implementation. Also, the class-wins rule means a superclass method could override them anyway.
Do default methods make interfaces the same as abstract classes? No. Interfaces still have no instance fields, no constructors and no non-public instance state. Use an abstract class when you need shared state.
Is calling a static method through an instance allowed for classes but not for interfaces?
Yes. For classes, obj.staticMethod() compiles (IDEs and -Xlint:static warn about it). For interfaces, a static method isn't a member of the implementing type at all, so it doesn't compile.
Summary
defaultmethods are public instance methods with bodies. Classes inherit them and may override them. They can't befinal,staticorabstract.- Conflicting defaults from unrelated interfaces force an override, and
X.super.m()reaches a direct super-interface's version. - A superclass's public method beats an interface default, and a more specific interface beats its parent.
staticinterface methods aren't inherited, so call them asInterface.m().privateandprivate staticmethods are helpers visible only inside the interface. Static members can't call instance members.
Practice quiz
Test yourself with the default, static and private methods quiz: 10 exam-style questions with explanations.
Previous: Java Interfaces: Declaring, Implementing and Inheriting Abstract Methods (OCP 21)
Next: Java Enums: Values, Constructors, Fields, Methods and switch (OCP 21)
Related reading
- Java Interfaces: Declaring, Implementing and Inheriting Abstract Methods (OCP 21)
- Java Immutable Objects and Defensive Copies (OCP 21)
- Java Abstract Classes and Abstract Methods (OCP 21)
- Java Method Overriding and Hiding: Rules, final Methods and Static Hiding (OCP 21)
- Java Initialization Order and final Fields (OCP 21)

More in OCP Java 21
OCP Java 21 Mock Exam 3 (1Z0-830)
The third full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), weighted toward the hardest areas: streams, concurrency, generics, modules and I/O. 120 minutes, 68% to pass, with explanations.
OCP Java 21 Mock Exam 2 (1Z0-830)
The second full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830): new questions on every objective, with more multi-select. 120 minutes, 68% to pass, with an explanation for every answer.
OCP Java 21 Mock Exam 1 (1Z0-830)
A full 50-question mock exam for the OCP Java SE 21 Developer exam (1Z0-830), covering every objective at exam difficulty. 120 minutes, 68% to pass, with an explanation for every answer.
Quiz: Final Review (OCP 21)
Ten exam-style questions mixing the most-tested OCP Java 21 rules from the final checklist: promotion, String immutability, overloading, initialization order, collections, streams, exceptions and switch patterns, with explanations.
Discussion
Sign in to ask a question or share what you learned.