An interface is a list of methods a class promises to have, without saying how they work. A class that implements the interface must provide every one of them. Interfaces describe what something can do, so very different classes can share one: a document and a photo can both be Printable without being related at all.
Java
public class Main {
interface Shape {
double area();
String name();
}
static class Circle implements Shape {
private final double r;
Circle(double r) { this.r = r; }
public double area() { return Math.PI * r * r; }
public String name() { return "circle"; }
}
static class Rectangle implements Shape {
private final double w, h;
Rectangle(double w, double h) { this.w = w; this.h = h; }
public double area() { return w * h; }
public String name() { return "rectangle"; }
}
static void report(Shape s) {
System.out.printf("%s: %.1f%n", s.name(), s.area());
}
public static void main(String[] args) {
report(new Circle(2));
report(new Rectangle(3, 4));
}
}
Prints
circle: 12.6
rectangle: 12.0
report takes any Shape. It doesn't know about circles or rectangles, only that whatever it gets has area() and name(). You can't make an object of an interface itself (new Shape() won't compile); you make objects of classes that implement it. Interface methods are public, so the class's versions must say public.
A class can extend only one class but implement many interfaces: class Duck extends Bird implements Swimmer, Flyer. That's the main reason interfaces exist.
Default methods
An interface can also give a method a ready-made body with default. Every class gets it for free, built on the methods it does provide, and can override it if it likes:
Java
public class Main {
interface Greeter {
String name();
default String greet() { return "Hi, I'm " + name(); }
}
static class Robot implements Greeter {
public String name() { return "R2"; }
}
static class Pirate implements Greeter {
public String name() { return "Anne"; }
@Override public String greet() { return "Ahoy! " + name() + " here"; }
}
public static void main(String[] args) {
Greeter[] gs = { new Robot(), new Pirate() };
for (Greeter g : gs) System.out.println(g.greet());
}
}
Prints
Hi, I'm R2
Ahoy! Anne here
Abstract classes
An abstract class sits between a class and an interface. It can have fields, constructors and finished methods like any class, plus abstract methods with no body that subclasses must fill in. Like an interface, you can't make one with new. Use one when related classes share real code and data, and only some steps differ:
Java
public class Main {
static abstract class Report {
private final String title;
Report(String title) { this.title = title; }
abstract String body();
String render() {
return "== " + title + " ==\n" + body();
}
}
static class SalesReport extends Report {
SalesReport() { super("Sales"); }
String body() { return "Up 4% this week"; }
}
public static void main(String[] args) {
System.out.println(new SalesReport().render());
}
}
Prints
== Sales ==
Up 4% this week
Code to the interface
Java's own libraries use interfaces everywhere. List is an interface; ArrayList and LinkedList are two classes that implement it. The usual habit is to write the variable's type as the interface and choose the class only where the object is made:
Java
List<String> names = new ArrayList<>();
names.add("Ana");
names.add("Ben");
System.out.println(names.size());
Prints
2
The rest of the code only uses what List promises, so switching to another kind of list later means changing one line. You'll meet List properly in the next lesson.
Interface or abstract class? Start with an interface: it's lighter, and a class can have many. Reach for an abstract class when several subclasses really share fields and code.
Practice
What does an interface contain?
More info
Quick reference
interface X { int m(); }
Methods a class promises to have.
class C implements X, Y
C provides X's and Y's methods (public).
default int m() { ... }
An interface method with a ready-made body.
abstract class
Can't be made with new; may have abstract methods.