CKTIS / Java
interface — type contracts
interface — type contracts: An interface declaration defines a reference type with member contracts; fields are public static final, while methods may be abstract, default, static, or private.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
interfaceAn interface declaration defines a reference type with member contracts; fields are public static final, while methods may be abstract, default, static, or private.
Use interfaces as small roles and dependency boundaries; one abstract method lets a lambda implement a functional interface.
An interface has no constructors or instance fields; two inherited default methods with the same contract can require a resolving override.
An interface can extend several interfaces, be sealed, or be nested; an interface reference can be null and invocation on it throws NullPointerException.
Main implements transform, default format calls the private helper wrap, and uppercasing Ada produces the exact output [ADA].
Examples
interface Formatter {
String transform(String value);
private static String wrap(String value) {
return "[" + value + "]";
}
default String format(String value) {
return wrap(transform(value));
}
}
public class Main implements Formatter {
public String transform(String value) {
return value.toUpperCase(java.util.Locale.ROOT);
}
public static void main(String[] args) {
System.out.println(new Main().format("Ada"));
}
}
Result
[ADA]
Main implements transform, default format calls the private helper wrap, and uppercasing Ada produces the exact output [ADA].