OBJECT-ORIENTED FOUNDATIONS / OBJECT DESIGN BRIEF

Abstraction

Abstraction means exposing what something does while hiding how it does it.

BeginnerPhase 01 / Topic 3 of 7ResponsibilitiesCollaborationsExtensibility
01

Overview

Abstraction means exposing what something does while hiding how it does it. A caller of PaymentGateway.charge() does not need to know whether it talks to Stripe or a bank, retries, or logs. Good abstractions model the essential behavior and leave out incidental details.

In code, abstraction appears as interfaces, abstract classes, and well-named methods. Encapsulation hides data; abstraction hides complexity and implementation choices. Choosing the right level of abstraction, not too generic and not too leaky, is one of the core skills in LLD.

A TV remote

You press 'volume up' without knowing about infrared codes or amplifier circuits. Any brand of TV can respond to the same buttons.

02

When to use it

  • Several implementations of the same capability (storage, notification, payment).
  • Callers should not depend on a vendor or technology.
  • Complex subsystems that need a simple entry point.
03

Where it shows up in interviews

Pluggable implementations

Recognize it when: support SMS, email, and push; or S3 and local disk.

  • Design a notification service
  • Design a file storage library
Hiding complexity

Recognize it when: callers repeat many low-level steps.

  • Design a logging framework
  • Design an ATM
04

Where it is used in real software

JDBC

Java code talks to java.sql.Connection; drivers for PostgreSQL or MySQL implement it.

Cloud SDK storage abstractions

Spring Resource or Node's streams let the same code read from disk, classpath, or network.

Operating systems

Files abstract away disks, SSDs, and network storage behind open/read/write.

05

Key terms

Interface
A contract of operations without implementation.
Abstract class
Partial implementation that subclasses complete.
Leaky abstraction
Implementation details that escape to callers.
Level of abstraction
How much detail a method or type exposes.
06

How it works, step by step

  1. 1
    Describe the capability in domain terms

    send(notification), not postToTwilio(json).

  2. 2
    Define the minimal contract

    Only operations every implementation supports.

  3. 3
    Hide vendor types

    Return your own result types, not SDK objects.

  4. 4
    Implement behind the contract

    EmailNotifier, SmsNotifier.

  5. 5
    Keep each method at one level

    High-level methods call other high-level methods.

07

Concrete vs abstract dependency

An order service needs to send confirmations

Step 1 / 3
ChangeDepends on TwilioClientDepends on Notifier interface
Switch to another SMS vendorEdit OrderServiceAdd a new Notifier implementation
Add email confirmationsMore vendor code in OrderServiceComposite or second Notifier
Unit test OrderServiceMock Twilio SDK internalsPass a fake Notifier

NOWChange: Switch to another SMS vendor | Depends on TwilioClient: Edit OrderService | Depends on Notifier interface: Add a new Notifier implementation

Depending on an abstraction isolates the service from vendor changes and simplifies tests.

08

Implementation

interface Notifier {  send(to: string, message: string): Promise<{ delivered: boolean }>;} class SmsNotifier implements Notifier {  constructor(private client: { messages: { create(o: object): Promise<{ status: string }> } }) {}  async send(to: string, message: string) {    const res = await this.client.messages.create({ to, body: message });    return { delivered: res.status !== "failed" }; // vendor type does not leak  }} class OrderService {  constructor(private notifier: Notifier) {}  async confirm(orderId: string, phone: string) {    await this.notifier.send(phone, `Order ${orderId} confirmed`);  }}
09

Complexity and performance

Runtime cost1 virtual call

Negligible.

Design costOne interface per capability

Only where variation exists.

10

Trade-offs

Flexibility vs indirection

Abstractions ease change but add types to navigate; do not abstract things that will never vary.

Lowest common denominator

A generic interface may hide useful features of specific implementations.

11

Variants and related techniques

Functional abstraction

Pass functions instead of interfaces for single-method behaviors.

Facade

A simple API over a complex subsystem.

12

Common mistakes

  • Premature abstraction.

    Fix: Wait for a second real implementation or a clear testing need.

  • Leaking implementation types.

    Fix: Map vendor exceptions and DTOs to your own types.

  • Mixing levels in one method.

    Fix: Split low-level details into helpers.

13

Interview questions

Abstraction vs encapsulation?

Encapsulation hides an object's data and protects its invariants; abstraction hides implementation complexity behind a simpler contract. They often work together.

What is a leaky abstraction?

One where callers must understand the hidden details to use it correctly, such as having to catch vendor-specific exceptions or know about retry behavior.

14

Practice problems

ProblemDifficultyWhat it trains
Abstract a FileStorage over disk and S3EasyContract design.
Design a PaymentGateway for 3 providersMediumMapping results and errors.