Abstraction

Abstraction shows callers what a component does and hides how. Choosing what to show is the central design decision in every class, interface and service boundary.

Overview

Abstraction is the act of naming a thing by what it does and leaving out how it does it. A caller of storage.save(key, bytes) does not know or care whether the bytes went to a local disk, S3 or memory. That ignorance is the point: the caller's code stays simple, the implementation can change, and two people can work on the two sides without talking every hour.

Every level of a program is an abstraction over the one below it. A function abstracts a sequence of statements. A class abstracts data plus the operations on it. An interface or abstract class abstracts a family of classes. A service abstracts a whole process behind an API. Interviewers are not testing whether you know the word; they are testing whether you choose the right things to hide, and whether you notice when a detail is leaking through.

Show what, hide how

The clearest way to see an abstraction is to look at what one class can see and what it cannot. Below, DocumentService holds a Storage and knows only its two verbs.

The same move happens at every level of a program, each time hiding something bigger.

  • A function: total_price(cart) hides the loop, the rounding and the tax table.
  • A class: BankAccount.withdraw(cents) hides the balance field, the overdraft rule and the audit log.
  • An interface or abstract class: Storage.save(key, data) hides S3 versus disk, retries and credentials.
  • A module or service: POST /payments hides the whole payment pipeline, its database and its vendors.

Interview questions

  • How do abstract classes and interfaces implement abstraction?
  • What is the difference between abstraction and encapsulation?
  • How do you choose the right level of abstraction?
  • What is a leaky abstraction, and how do you design for it?
  • When is abstraction harmful?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio