Abstraction exposes a useful operation while keeping implementation choices behind a small contract. In object-oriented programming, interfaces and abstract classes can express those contracts, but a normal function can also be an abstraction. The key question is which details callers truly need—and which changes should not force every caller to be rewritten.
An interface defines a capability
This Java example lets a caller work with a Notifier without knowing the concrete output. The class using the interface does not read the implementation’s internal fields.
interface Notifier { void send(String text); }
class ConsoleNotifier implements Notifier {
public void send(String text) { System.out.println(text); }
}
public class Demo {
static void alert(Notifier n) { n.send("ready"); }
public static void main(String[] args) { alert(new ConsoleNotifier()); }
}
Abstraction is not just access restriction
Marking fields private is encapsulation. It can support abstraction by hiding representation, but merely hiding a field does not guarantee the public methods express a meaningful contract. A useful interface tells callers what the operation does, which inputs are valid and how failure is reported.
Abstract class versus interface
An interface describes capabilities; an abstract class can also hold shared implementation and state. Use a shared base implementation when concrete types genuinely need the same lifecycle or invariants. Avoid forcing unrelated classes into a hierarchy just to reuse one helper method.
Composition keeps dependencies replaceable
An application service can receive an interface reference in its constructor and use it when needed. Tests can provide a harmless fake that records calls. This is a practical gain from abstraction: a unit test can verify behavior without sending real network notifications.
Know when abstraction becomes expensive
Extra interfaces create names, files and indirection. One concrete implementation with no planned variation may be clearer than five layers of adapters. Introduce an abstraction where it isolates change or a valuable test boundary, not because every class must have an interface.
Test the contract from a client perspective
Write tests that exercise valid inputs, invalid inputs and at least two implementations if they exist. Check that error handling and side effects are consistent. An abstraction that behaves differently in undocumented ways can be harder to use than the concrete API it replaced.
Takeaway
Good abstraction is selective: show the operations a caller needs, hide representation and resource ownership details, and keep failure behavior explicit. Combine it with encapsulation and composition instead of treating inheritance as the only path to object-oriented design.