# Abstraction: Hiding Complexity

> What is abstraction in OOP? How do you achieve it in Java and how is it different from encapsulation?

Canonical: https://www.internhack.in/learn/interview/oop-interview/oop-abstraction
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What is abstraction in OOP? How do you achieve it in Java and how is it different from encapsulation?

## Answer

Abstraction is one of those words everyone knows but few can explain without falling back on a dictionary definition, so let me give you a practical way to think about it.

Consider your phone's camera app. You tap one button and get a photo. You do not choose shutter speed, adjust the sensor gain, or run noise reduction. All of that complexity is hidden behind a simple interface. Abstraction means exposing what an object does and hiding how it does it, so users of the object deal with a small, meaningful set of operations instead of the messy details.

In software, abstraction has two big payoffs. First, callers become simpler because they only learn the essential operations. Second, and more important for engineers, the implementation can change freely as long as the contract stays the same. Your app can switch from sending OTPs over one SMS vendor to another without a single change in the login code, because the login code only knows about a Notifier abstraction.

In Java you achieve abstraction in two ways. Abstract classes, declared with the abstract keyword, can contain abstract methods without a body plus concrete methods and state. Interfaces declare a pure contract; since Java 8 they can also carry default and static methods. You cannot instantiate either directly, but you can hold a reference of that type and let any implementing subclass be plugged in. In C++, the same is done with pure virtual functions (virtual void f() = 0), and in Python through the abc module with ABC and abstractmethod.

Now the difference from encapsulation, which is a favourite follow-up. Abstraction is a design-level concept about deciding which details matter to the user and which should be invisible. Encapsulation is an implementation-level concept about bundling data with methods and restricting access to the data. A neat one-liner: abstraction is about what you show, encapsulation is about how you protect what is inside. They cooperate: you hide the fields (encapsulation) so that you can present a clean, behaviour-oriented interface (abstraction).

Notice the levels of abstraction as well. A List interface abstracts over ArrayList and LinkedList. JDBC abstracts over dozens of database drivers. The operating system abstracts hardware into files and processes. Good engineers layer abstractions so that each layer only talks to the one directly below it.

A caution worth mentioning: abstraction has a cost. Premature or leaky abstractions create indirection without benefit. Do not create an interface for every class; introduce one when you have, or clearly expect, multiple implementations, or when you need to decouple a boundary such as a database, a payment gateway, or an external API.

In the example below, alert depends only on Notifier. Adding a WhatsApp implementation later requires zero change to alert.

## Code examples

### Program to an interface, not an implementation

```
interface Notifier {
    void send(String to, String message);
}

class EmailNotifier implements Notifier {
    public void send(String to, String message) {
        System.out.println("Email to " + to + ": " + message);
    }
}

class SmsNotifier implements Notifier {
    public void send(String to, String message) {
        System.out.println("SMS to " + to + ": " + message);
    }
}

public class AbstractionDemo {
    static void alert(Notifier n, String to) {
        n.send(to, "Your OTP is 4821");   // caller has no idea how delivery works
    }

    public static void main(String[] args) {
        alert(new EmailNotifier(), "asha@example.com");
        alert(new SmsNotifier(), "9876543210");
    }
}
```

The alert method depends on the abstraction; implementations are swappable.

### Abstract base classes in Python

```
from abc import ABC, abstractmethod

class Shape(ABC):
    @abstractmethod
    def area(self): ...

try:
    Shape()
except TypeError:
    print("Cannot instantiate abstract class")
```

## Key points

- Abstraction exposes what an object does and hides how it does it.
- Java achieves it with abstract classes and interfaces; C++ with pure virtual functions.
- Abstraction is design-level (what to show); encapsulation is implementation-level (how to protect data).
- Do not over-abstract; add an interface when there are multiple implementations or a boundary to decouple.

## Common follow-up questions

- Can an abstract class have a constructor?
- Can we achieve 100% abstraction with abstract classes?
- What is the difference between abstraction and information hiding?
- Give a real project example where you used abstraction.

## Interview tips

- Use the camera or car-driving analogy, then move to interface-based code within two sentences.
- Say clearly that abstraction is about design and contracts, not about the private keyword.
- Be ready with one real example from your own project, such as a payment or notification interface.

## Concepts covered

abstraction, interface, abstract-class, contract

Previous: [Encapsulation and Data Hiding](https://www.internhack.in/learn/interview/oop-interview/oop-encapsulation) · Next: [Inheritance and Its Types](https://www.internhack.in/learn/interview/oop-interview/oop-inheritance-types) · [All of OOP Concepts](https://www.internhack.in/learn/interview/oop-interview)

## More OOP Concepts questions

- [Polymorphism: Compile-time vs Run-time](https://www.internhack.in/learn/interview/oop-interview/oop-polymorphism)
- [Constructors and Destructors](https://www.internhack.in/learn/interview/oop-interview/oop-constructors-destructors)
- [Method Overloading vs Overriding](https://www.internhack.in/learn/interview/oop-interview/oop-overloading-vs-overriding)
- [Access Modifiers in Java and C++](https://www.internhack.in/learn/interview/oop-interview/oop-access-modifiers)
- [this and super Keywords](https://www.internhack.in/learn/interview/oop-interview/oop-this-super-keywords)
- [Static vs Dynamic Binding](https://www.internhack.in/learn/interview/oop-interview/oop-static-vs-dynamic-binding)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [OOP Concepts](https://www.internhack.in/learn/interview/oop-interview)
