Sub topic 3
Interfaces That Extend Other Interfaces
Interfaces Building on Interfaces
An interface can extend another interface with extends. The new interface gets every method of the old one and can add more. A class that implements the new interface must supply all of them.
Default, Static, and Private Methods
Since Java 8, interfaces can have extra kinds of methods:
- A default method has a body. Classes get it for free but can override it.
- A static method belongs to the interface itself. You call it as
Greeter.of(...). - A private method (Java 9 and later) is a helper that only the interface's own methods can call.
interface Named {
String name();
}
interface Greeter extends Named {
default String greet() {
return hello(name());
}
static Greeter of(String n) {
return () -> n;
}
private String hello(String n) {
return "Hello, " + n;
}
}
public class Main {
public static void main(String[] args) {
Greeter g = Greeter.of("Ada");
System.out.println(g.greet()); // prints: Hello, Ada
}
}When Two Defaults Collide
Suppose a class gets the same default method from two interfaces. Java will not guess which one to use, so the class must override the method. Inside the override, you pick a version with InterfaceName.super.method().
interface Walker { default String move() { return "walk"; } }
interface Swimmer { default String move() { return "swim"; } }
class Duck implements Walker, Swimmer {
@Override
public String move() {
return Walker.super.move() + " and " + Swimmer.super.move();
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Duck().move()); // prints: walk and swim
}
}The Template Method Pattern
The template method pattern is a design where an abstract class has a final method that sets the order of steps. Subclasses fill in only the steps marked abstract. The overall plan stays fixed, and the details can change from one subclass to the next.
Functional Interfaces and Lambdas
A functional interface has exactly one abstract method. A lambda is a short way to write that one method, for example (a, b) -> a + b. Add @FunctionalInterface and the compiler will check that the interface has only one abstract method. Greeter above is a functional interface. Its one abstract method is name(), which is why () -> n works.
Sealed Classes and Interfaces
A sealed type (standard since Java 17) lists the only classes allowed to extend it: sealed interface Vehicle permits Car, Truck {}. Each permitted class must be marked final, sealed, or non-sealed.
Abstract Class or Interface?
| Need | Choose |
|---|---|
| Shared fields or a constructor | Abstract class |
| A class that fits several roles | Interfaces |
| A fixed series of steps | Abstract class (template method) |
| One method, written as a lambda | Functional interface |
Exercise
A class implements two interfaces that both have a default method String id(). What must the class do, and how can it reuse the first interface's version?
Show answer
The class must override id(). Inside the override, it can call FirstInterface.super.id() to use that interface's version.