When does an abstract class fit better than an interface, and what can it give you that an interface cannot?
An abstract class fits better when the children have something in common that is more than a list of method names. So when they share code, share state, or you want to control them.
public class Main {
public static void main(String[] args) {
new EmailReport("weekly").run();
new SmsReport("weekly").run();
}
}
And here is the abstract class and the two children.
public abstract class Report {
private final String name;
protected Report(String name) {
this.name = name;
}
// The shared code. Every child gets it for free.
public void run() {
String body = buildBody();
deliver(body);
System.out.println("report " + name + " sent");
}
// The parts the children must fill in.
protected abstract String buildBody();
protected abstract void deliver(String body);
}
public class EmailReport extends Report {
public EmailReport(String name) {
super(name);
}
@Override
protected String buildBody() {
return "the body of the report";
}
@Override
protected void deliver(String body) {
System.out.println("emailing " + body);
}
}
So what can an abstract class give you that an interface cannot? Four things.
the thing why it matters
--------------------------------- --------------------------------------------
shared state the children can use the same
fields
shared code the children inherit the
behaviour and do not repeat it
a constructor the children must go through your
setup
protected access the children can reach the
internals of the base
And the first one is the biggest difference in practice. A class that extends an abstract class can use the protected fields of the base class directly. A class that implements an interface cannot, because the interface has no fields.
public abstract class Report {
private final java.util.List<String> history = new java.util.ArrayList<>();
protected void record(String line) {
history.add(line);
}
protected int historySize() {
return history.size();
}
}
public class EmailReport extends Report {
public void doWork() {
record("built the body");
// The child can use the base class's state.
System.out.println(historySize());
}
}
And there is one more thing that the abstract class gives you, and it is about control. The abstract class decides what the children must do and in what order. So the run method above is fixed and the children only fill in the parts. With an interface there is no such control, because the caller decides the order of the calls.
the thing an abstract class an interface
------------------------ ------------------------ ---------------------
who decides the order the base class does the caller does
shared state yes no
the children must you can leave methods no, the class decides
fill in some parts abstract
So the rule to remember is this. Use an abstract class when you want to give the children both the code and the order.
The last question in this topic is about what an abstract class can do that an interface cannot.