Abstraction
Abstraction hides how a type works and shows only what a caller may do. In Java that surface is usually an interface or an abstract class.
Detailed Explanation
A caller who sends email does not need SMTP sockets, retries, or MIME encoding. They need send(to, subject, body). Abstraction is the act of publishing that smaller surface and keeping the rest private. The point is not fewer lines. The point is fewer things a caller can depend on, so you can change the hidden parts.
Java gives two type-level tools. An interface names operations and, since Java 8, can supply default and static methods. An abstract class can hold fields and constructors, and can mark some methods abstract so subclasses must fill them in. Use an interface when you want many unrelated implementations. Use an abstract class when implementations share state or a construction ritual.
Abstraction also lives below types. A well-named method is an abstraction: charge(card, paise) hides the payment gateway. A package is an abstraction: only public types escape. Access modifiers are how you enforce the hiding. Without them, abstraction is a comment.
You reach for a new abstraction when the same sequence of steps appears in several places, or when a dependency (a database, a clock, a mail server) must be replaced in tests. You do not reach for one when there is a single implementation and no likely second. An interface with one class and one method is a file you will open forever to see what actually happens.
Good abstractions leak on purpose at the right layer. A file API that pretends disks cannot fail is a lie. A repository that returns domain objects and throws a domain exception is honest. The "abstraction leak" complaint usually means the hidden details showed up in the caller's types (SQLException on a service method) or in surprising performance (List.get is O(1) on ArrayList and O(n) on LinkedList).
The cost of too much abstraction is navigation. Five interfaces, three abstract classes, and a factory later, a beginner cannot find the line that writes to the database. The cost of too little is rigidity: every caller imports Hibernate types, and you cannot swap the store. Start concrete. Extract an interface when a second implementation or a test double appears.
Code Examples
interface Mailer {
void send(String to, String subject, String body);
}
class ConsoleMailer implements Mailer {
@Override
public void send(String to, String subject, String body) {
// Callers never see how we record the message.
System.out.println(to + " | " + subject + " | " + body);
}
}
class SignupService {
private final Mailer mailer;
SignupService(Mailer mailer) {
this.mailer = mailer; // depend on the abstraction, not ConsoleMailer
}
void welcome(String email) {
mailer.send(email, "Welcome", "Your account is ready.");
}
public static void main(String[] args) {
new SignupService(new ConsoleMailer()).welcome("a@b.com");
// prints: a@b.com | Welcome | Your account is ready.
}
}abstract class Report {
final String title;
Report(String title) {
this.title = title;
}
// Shared skeleton. Subclasses fill in the one varying step.
final String render() {
return "# " + title + "\n" + body();
}
abstract String body();
}
class DailyReport extends Report {
DailyReport() {
super("Daily");
}
@Override
String body() {
return "ok";
}
public static void main(String[] args) {
System.out.println(new DailyReport().render());
// prints:
// # Daily
// ok
}
}Common Gotchas
One-implementation interfaces add noise
extract when a second implementation or a test double exists, not on the first class.
Returning concrete types undoes the abstraction
a method declared to return `ArrayList` cannot later return a `List` from `List.of` without breaking callers.
`abstract` classes can have constructors
they run when a subclass is constructed, even though you cannot write `new Report(...)`.
Leaking checked exceptions
declaring `SQLException` on a service method forces every caller to know about JDBC.