Interfaces, and a whole program
An interface's box
An interface (module 35) gets a box like a class, with «interface» above its name. It has no instance variables and no constructor, so below its name, its box lists only the methods.
The recycling center's weighing station weighs anything that can say what it weighs, so it has an interface Weighable. A crate of bottles and a bale of paper both implement it:
The diagram as text
- Weighable, an interface
- public method
weight(): int
- public method
- Crate, a class that implements Weighable
- private field
bottles: int - public constructor
Crate(bottles: int) - public method
weight(): int
- private field
- Bale, a class that implements Weighable
- private field
kilos: int - public constructor
Bale(kilos: int) - public method
weight(): int
- private field
- The interface's methods are public,
+. The interface only lists them, so a line for each is all there is to show. - A class that implements the interface is joined to it by a dashed line with a hollow triangle at the interface: the triangle of
extends, dashed forimplements. - Each class's box lists the interface's methods again, since each class writes them.
- A class that implements several interfaces has a dashed line to each of them.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Weighable> station = new ArrayList<>();
station.add(new Crate(24));
station.add(new Bale(250));
int total = 0;
for (Weighable item : station) {
total += item.weight();
}
System.out.println(total + " kg");
}
}public interface Weighable {
int weight();
}public class Crate implements Weighable {
private int bottles;
public Crate(int bottles) {
this.bottles = bottles;
}
@Override
public int weight() {
return 2 + this.bottles / 2;
}
}public class Bale implements Weighable {
private int kilos;
public Bale(int kilos) {
this.kilos = kilos;
}
@Override
public int weight() {
return this.kilos;
}
}264 kgA whole program
A diagram of a whole program uses everything at once. Here's the weighing station with a Scale that remembers what it has weighed, and old tires, from step 6, that can be weighed as well:
The diagram as text
- Weighable, an interface
- public method
weight(): int
- public method
- DropOff, an abstract class
- private field
kilos: int - public constructor
DropOff(kilos: int) - public abstract method
fee(): int - more members, not shown
- private field
- Tires, a class that extends DropOff and implements Weighable
- private field
count: int - public constructor
Tires(kilos: int, count: int) - public method
fee(): int - public method
weight(): int
- private field
- Crate, a class that implements Weighable
- private field
bottles: int - public constructor
Crate(bottles: int) - public method
weight(): int
- private field
- Scale, a class
- public constructor
Scale() - public method
weigh(item: Weighable): void - public method
total(): int
- public constructor
Connections:
- Each Scale has any number of Weighable objects (weighed).
To read a diagram like this, go through it in layers:
- The boxes. What each one is: a class, an abstract class («abstract») or an interface («interface»).
- The triangles. Each solid one is an
extends, each dashed one animplements. Tires does both: it extends DropOff and implements Weighable. - The arrows. Each is a field, and a star makes it a list. The Scale keeps a list of Weighables: it can weigh crates and tires alike, and any class that implements Weighable later.
- The members, box by box. A line
...stands for members that the diagram leaves out, as in DropOff's box: step 6 showed the rest.
To write the program, start with what needs no other class of the diagram: here the interface and DropOff. Then write the classes that extend or implement them, Tires and Crate, and last the classes whose arrows point at the others, the Scale. Each file then compiles as soon as it's written, since everything it names is already there.