10. Interfaces
An **interface** declares behavior to be implemented. A class implements one or more interfaces with `implements`. Since JDK 8 interfaces can carry `default` and `static` methods too — the underpinning of the Stream and functional APIs.
What you'll learn
- 1Declare an interface and implement it with `implements`
- 2Implement multiple interfaces in one class
- 3Use `default` methods to provide a default implementation
- 4Understand a functional interface (single abstract method)
Overview
An **interface** declares behavior to be implemented. A class implements one or more interfaces with `implements`. Since JDK 8 interfaces can carry `default` and `static` methods too — the underpinning of the Stream and functional APIs.
Core Concepts
1) Declaring an interface
interface Drawable {
void draw(); // implicitly public abstract
}2) `implements`
class Circle implements Drawable {
@Override
public void draw() { System.out.println("◯"); }
}3) Multiple interfaces
class Sword implements Drawable, Comparable<Sword> { ... }A class can implement many interfaces, but extend only one parent class.
4) `default` and `static` (JDK 8+)
interface Greeter {
String name();
default String greet() { // implementation
return "Hello, " + name() + "!";
}
static Greeter of(String n) { // interface-level static
return () -> n;
}
}5) Functional interface
An interface with **exactly one abstract method** is called a functional interface. Lambdas can be assigned to it. The `@FunctionalInterface` annotation is optional but documents intent.
@FunctionalInterface
interface Op { int apply(int x); }
Op square = x -> x * x;
System.out.println(square.apply(5)); // 25Examples
Example 1 — `DrawableDemo.java`: basic interface
public class DrawableDemo {
interface Drawable { void draw(); }
static class Circle implements Drawable {
@Override public void draw() { System.out.println("◯"); }
}
static class Square implements Drawable {
@Override public void draw() { System.out.println("□"); }
}
public static void main(String[] args) {
Drawable[] shapes = { new Circle(), new Square() };
for (Drawable d : shapes) d.draw();
}
}**Output**
◯
□**Note:** the contract lives in `Drawable`, while each class chooses its own implementation.
Example 2 — `MultipleImpl.java`: multiple interfaces
public class MultipleImpl {
interface Run { void run(); }
interface Swim { void swim(); }
static class Duck implements Run, Swim {
@Override public void run() { System.out.println("waddle"); }
@Override public void swim() { System.out.println("paddle"); }
}
public static void main(String[] args) {
Duck d = new Duck();
d.run();
d.swim();
}
}**Output**
waddle
paddleExample 3 — `DefaultMethod.java`: `default` and `static`
public class DefaultMethod {
interface Greeter {
String name();
default String greet() { return "Hello, " + name() + "!"; }
static Greeter of(String n) { return () -> n; }
}
public static void main(String[] args) {
Greeter g = Greeter.of("World");
System.out.println(g.greet());
}
}**Output**
Hello, World!**Note:** `default` lets you evolve interfaces without breaking existing implementations.
Example 4 — `Functional.java`: functional interface + lambda
public class Functional {
@FunctionalInterface
interface Op { int apply(int x); }
public static void main(String[] args) {
Op square = x -> x * x;
Op inc = x -> x + 1;
System.out.println(square.apply(5));
System.out.println(inc.apply(5));
}
}**Output**
25
6**Note:** a lambda is shorthand for implementing a functional interface.
Common Mistakes
- Forgetting `public` on the implementation (interface members are `public` by default)
- Trying to put state (fields) in an interface — only constants (`public static final`) are allowed
- Diamond problem with conflicting `default` methods: the implementor must override and pick
- Marking a class `@FunctionalInterface` when it has more than one abstract method (compile error)
- Confusing abstract class vs interface — interface = pure contract, abstract class = contract + partial implementation
Summary
- Interface = behavior contract
- A class extends one parent but can implement many interfaces
- `default` and `static` make interfaces practical to evolve
- Functional interfaces + lambdas (lecture 18) are how modern Java composes behavior
Practice
# Practice - 10. Interfaces
## Exercise 1 — `Sortable` interface
- File: `Homework01.java`
- Key concepts: interface, override
Requirements
- Interface `Sortable` with `int order();`.
- Class `Task(String name, int order) implements Sortable`.
- In main create three tasks, sort by `order()` with `Arrays.sort(tasks, Comparator.comparingInt(Sortable::order))`, print in order.
Expected output
1: bake
2: cool
3: eat## Exercise 2 — `Calculator` functional interface
- File: `Homework02.java`
- Key concepts: functional interface, lambda
Requirements
- Functional interface `Calc { int compute(int a, int b); }`.
- Lambdas for `add` / `sub` / `mul`.
- Print `1 + 2 = 3`, `5 - 2 = 3`, `4 * 3 = 12`.
Expected output
1 + 2 = 3
5 - 2 = 3
4 * 3 = 12## Solutions After trying it yourself, compare with [`answer/`](./answer/).
Solution code (homework/answer/)
answer/Homework01.java
import java.util.Arrays;
import java.util.Comparator;
/** Sortable interface. */
public class Homework01 {
interface Sortable { int order(); }
record Task(String name, int order) implements Sortable {}
public static void main(String[] args) {
Task[] tasks = { new Task("cool", 2), new Task("eat", 3), new Task("bake", 1) };
Arrays.sort(tasks, Comparator.comparingInt(Sortable::order));
for (Task t : tasks) System.out.println(t.order() + ": " + t.name());
}
}
answer/Homework02.java
/** Calculator functional interface. */
public class Homework02 {
@FunctionalInterface
interface Calc { int compute(int a, int b); }
public static void main(String[] args) {
Calc add = (a, b) -> a + b;
Calc sub = (a, b) -> a - b;
Calc mul = (a, b) -> a * b;
System.out.println("1 + 2 = " + add.compute(1, 2));
System.out.println("5 - 2 = " + sub.compute(5, 2));
System.out.println("4 * 3 = " + mul.compute(4, 3));
}
}
Try It Yourself
cd 02_oop/10_interface/src
javac DefaultMethod.java
java DefaultMethodNext Lecture
[11_Arrays](../../03_컬렉션/11_배열/) — the next chapter starts: arrays and multi-dimensional arrays, then the Collections framework.
All lecture materials and example code are openly available on GitHub.
View on GitHub ↗