8.1 What an Interface Really Is
An interface is a pure contract: a type that says what an implementer can do, with no instance state. Implicit modifiers examiners test:
- Fields are always
public static final(constants) — you cannot have instance variables. - Abstract methods are always
public abstract(writing them is optional/redundant). - Interfaces have no constructors and cannot be instantiated; a class
implementsthem, an interfaceextendsother interfaces (even several at once).
interface Payable {
double TAX = 0.18; // public static final implicitly
double amountDue(); // public abstract implicitly
}
A class may implements many interfaces — this is Java's multiple inheritance of type. It is safe because interfaces carry no state; only behaviour can conflict, and conflicts must be resolved explicitly (§8.3).
8.2 Evolution: default, static & private Methods
| Member kind | Since | Purpose | Called via |
|---|---|---|---|
| Abstract method | 1.0 | The contract | Implementing object |
default method | 8 | Evolve interfaces without breaking implementers | Object (inheritable, overridable) |
static method | 8 | Utility helpers | InterfaceName.method() only — not inherited |
private method | 9 | Share code between defaults | Inside the interface only |
default methods exist for backward compatibility: when Java 8 added forEach to Iterable, every existing collection class would have broken without a default body.
8.3 The Diamond Problem — and Java's Resolution Rules
interface A { default String hi() { return "A"; } }
interface B { default String hi() { return "B"; } }
class C implements A, B { // conflict! compiler FORCES an override
@Override public String hi() {
return A.super.hi() + B.super.hi(); // explicitly pick/combine parents
}
}
Resolution rules, in order: (1) a superclass method always beats interface defaults ("class wins"); (2) the most specific sub-interface default wins; (3) otherwise the class must override and may delegate with X.super.method().
8.4 Functional Interfaces — the Lambda Target
A functional interface has exactly one abstract method (SAM). default/static methods don't count, nor do abstract declarations of Object methods like equals.
@FunctionalInterface
interface Calculator { int apply(int a, int b); }
Calculator add = (a, b) -> a + b; // lambda implements the SAM
Calculator max = Math::max; // method reference
System.out.println(add.apply(3, 4)); // 7
@FunctionalInterface is optional but makes the compiler enforce the single-abstract-method rule. Built-ins you must know: Runnable (run), Comparator<T> (compare), Callable<V> (call), and the java.util.function family — Predicate<T> (test→boolean), Function<T,R> (apply), Consumer<T> (accept), Supplier<T> (get) — detailed in Lesson 14.
8.5 Marker (Tag) Interfaces
A marker interface is completely empty; implementing it tags a class with metadata the JVM or libraries check via instanceof:
Serializable— permits object serialization (Lesson 13); ObjectOutputStream throwsNotSerializableExceptionotherwise.Cloneable— permitsObject.clone(); without it you getCloneNotSupportedException.RandomAccess— signals O(1) indexed access (ArrayList has it, LinkedList doesn't).
Modern alternative: annotations — but markers participate in the type system (void save(Serializable s)), which annotations cannot.
8.6 Interface vs Abstract Class — Quick Contrast
Interfaces: no state, no constructors, multiple inheritance, implicit public members, model a capability. Abstract classes: fields + constructors + any access level, single inheritance, model an is-a core with shared implementation. (Full table in Lesson 7.)
🎯 Exam Focus
- List the implicit modifiers of interface fields and methods. Why can't an interface have instance variables or constructors?
- How do interfaces provide multiple inheritance in Java while classes cannot? Explain the diamond problem and Java's three resolution rules with
X.super.m(). - Why were default methods added in Java 8? Differentiate default and static interface methods.
- What is a functional interface? What does
@FunctionalInterfaceenforce? Give four built-in examples. - What is a marker interface? Name three from the JDK and explain how the JVM uses one of them.
- Write a program where class SmartPhone implements interfaces Camera and Phone that both declare a default
spec()method, resolving the conflict explicitly.