Sub topic 18
Abstract Class or Interface? Making the Choice
You now know both tools. An abstract class is a class you cannot create objects from, and it can hold fields and code that subclasses share. An interface is a list of methods that a class promises to provide. Both describe what a type does. This chapter helps you pick the right one.
The Key Differences
| Question | Abstract class | Interface |
|---|---|---|
| How many can a class use? | Only one (`extends`) | Many (`implements`) |
| Can it have instance fields? | Yes | No, only constants |
| Can it have a constructor? | Yes | No |
| Can it have method bodies? | Yes | Yes, using `default`, `static`, and `private` methods |
| Method access | Any modifier | Abstract methods are always `public` |
The biggest difference is in the first two rows. A class can extend only one parent, but it can implement as many interfaces as it needs. Also, only an abstract class can store state. State is the data an object keeps in its fields.
A Simple Rule of Thumb
- Choose an abstract class when related classes share data and code. For example, every
Employeehas a name. - Choose an interface when unrelated classes share a capability, meaning something they can do. An employee and an invoice are very different things, yet both can be paid.
- When in doubt, start with an interface. Interfaces leave classes free to extend something else later.
Using Both Together
You do not have to choose only one. A common design uses an interface for the capability and an abstract class for shared code.
interface Payable {
double amountDue();
}
abstract class Employee implements Payable {
private final String name;
protected Employee(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class SalariedEmployee extends Employee {
private final double monthlySalary;
SalariedEmployee(String name, double monthlySalary) {
super(name);
this.monthlySalary = monthlySalary;
}
@Override
public double amountDue() {
return monthlySalary;
}
}
class Invoice implements Payable {
private final double total;
Invoice(double total) {
this.total = total;
}
@Override
public double amountDue() {
return total;
}
}
public class Main {
public static void main(String[] args) {
Payable[] bills = { new SalariedEmployee("Ana", 3000.0), new Invoice(250.0) };
double sum = 0;
for (Payable p : bills) {
sum += p.amountDue();
}
System.out.println("Total due: " + sum); // prints: Total due: 3250.0
}
}Employee holds the shared name field and its constructor. Payable lets an Invoice, which is not an employee, join the same loop. Employee does not write amountDue(), which is allowed because it is abstract. Each concrete subclass must supply it.
Exercises
Exercise
You are modeling Dog, Cat, and Horse. Each one has a name and an age, and the code that sets them is the same. Should you use an abstract class or an interface for the common parent, and why?
Show answer
Use an abstract class, such as Animal. The three classes are closely related, and they share fields (name and age) and constructor code. Only an abstract class can hold instance fields and a constructor.
Exercise
Document, Photo, and Spreadsheet are unrelated classes. Each one already extends a different parent class, and each one needs a print() method. What should you use?
Show answer
Use an interface, such as Printable, with a print() method. Each class already extends a parent, and Java allows only one superclass, but a class can implement any number of interfaces.