When you book a cab you pick a pickup point, a drop point and a way to pay, and then you tap one button. Behind that button the app works out the fare, finds a driver, talks to a payment gateway and keeps you posted. You see none of it, and you do not need to. Abstraction is the idea of giving the user, or another part of the program, a simple view of something complicated.

In one line: show what an object can do, and hide how it does it.

Ordering food

On a delivery app, the only thing you deal with is the Place Order button. Charging your UPI, sending the order to the restaurant, assigning the nearest rider and sending the SMS updates all happen behind it. The app could switch payment providers or rewrite its rider matching tomorrow, and you would still just tap Place Order.

1. What Is Abstraction?

Abstraction means describing a thing by the behavior that matters and leaving out the details the caller does not need. In code it is the line between the contract, meaning the methods a caller can rely on, and the implementation, meaning the code that actually does the work.

You find it at every level of a design. A single method like brew("Latte") hides heating, grinding and frothing. A type like Ride hides whether the ride is a bike, an auto or a cab. A contract like PaymentMethod hides whether the money comes from UPI, a card or a wallet.

The caller writes code against the what. The how is free to change behind it.

2. Why Abstraction Matters

It is easiest to see by looking at a design without it. Here a ride-hailing app works out fares in one class that knows about every ride type:

Output

Auto, 8 km: Rs.110
Error: Unknown ride type: AUOT

That is fine for three ride types, but it falls apart as the app grows. Every new type (an EV cab, an XL cab, a rental) means opening the same method again and risking the branches that already work. The ride type is a plain string, so a typo like "AUOT" compiles happily and only fails when the code runs. And every class that needs a fare has to know about every ride type, so the pricing logic is tangled up with everything else.

Abstraction turns this around. Each ride type knows its own pricing, and the rest of the app only talks to a general Ride. Code that accepts a Ride now works with a bike, an auto or a cab. An EvCabRide becomes one new class, with no existing class edited (this is the Open/Closed Principle). The booking code calls ride.fare() and never sees a base fare or a per-km rate. And the fare formula is written once, in the abstract class, instead of once per type.

3. How Abstraction Is Achieved

Java gives you three tools for this: abstract classes, interfaces, and plain classes with a clean public API.

ToolJava
Abstract classabstract class
Abstract methodabstract int fare();
Interfaceinterface

3.1 Abstract Classes

An abstract class describes a family of related classes. It can have abstract methods, which are declared but left for each subclass to write, and ordinary methods, which are written once and inherited by every subclass. You cannot create an object of an abstract class directly.

In our app every ride is priced the same way, base fare plus per-km rate times distance. Only the numbers change. So the formula goes into the abstract Ride once, and each subclass just supplies its numbers. In the diagram, * marks the abstract methods and # marks the protected ones.

Output

Bike, 5 km: Rs.50
Auto, 8 km: Rs.110
Cab, 12 km: Rs.230

fare() is final, so no subclass can change how a fare is calculated; it can only provide its own rates. A base class that fixes the steps while subclasses fill in the details is called the Template Method pattern. It comes up again in the design patterns section.

3.2 Interfaces

An interface is a contract and nothing else: a list of methods, with no shared code and no state. It describes something an object can do ("can pay") rather than what family it belongs to ("is a ride"). UPI, cards and wallets work in completely different ways and share no code at all, but every one of them can pay.

Output

Paid Rs.80 via UPI (ravi@okbank)
Charged Rs.80 to card ending 4821
Paid Rs.80 from wallet, Rs.20 left

Anything that needs to take a payment depends on PaymentMethod and nothing more. Adding Net Banking or Pay Later is one new class, and none of the code that uses PaymentMethod has to change.

3.3 A Clean Public API

You do not always need inheritance to get abstraction. An ordinary class with a few clear public methods and private internal steps does the job too. A coffee machine gives the user one button; the heating, grinding and frothing stay inside.

Output

Latte is ready
Sorry, Masala Chai is not on the menu

The caller only knows about brew(). The machine could change its water temperature, add a cleaning cycle or get a new grinder, and the calling code would stay exactly the same.

4. Abstract Class vs Interface: When to Use Which

Abstract classInterface
DescribesA family: "is a" (a CabRide is a Ride)A capability: "can do" (a wallet can pay)
Shared codeYes, concrete methods are inheritedNo shared state (Java allows default methods for small helpers)
Fields / stateYes (distanceKm)No instance fields
ConstructorYes, to validate shared fieldsNo
How many per classA class extends only one (in Java)A class can implement many
Use it whenRelated classes share real logic or dataUnrelated classes need a common contract

A good habit is to start with an interface, and move to an abstract class only when several implementations really do share code or state.

5. Abstraction vs Encapsulation

People mix these two up because both of them hide something. The difference is what they hide, and why.

AbstractionEncapsulation
HidesComplexity: how something worksData: the object's internal state
Question it answers"What should the caller see?""Who is allowed to change this data?"
ViewpointOutside view, design levelInside view, implementation level
Main toolsAbstract classes, interfaces, simple public methodsAccess modifiers, validation, read-only views
Examplebrew("Latte") hides the brewing stepsbalance is private in MetroCard

They work as a pair. Abstraction decides what to show; encapsulation makes sure everything else stays hidden.

6. Practical Example: Ride Booking System

Here is the scenario. A booking service has to accept any ride type and any payment method, work out the fare, take the payment and confirm the booking. New ride types and payment methods get added every few months.

RideBookingService depends only on the two abstractions, Ride and PaymentMethod. It never names a concrete class.

This is what happens when someone books a 12 km cab and tries to pay from a wallet that only has Rs.150:

Output

Bike ride, 5 km, fare Rs.50, paying by UPI
Paid Rs.50 via UPI (ravi@okbank)
Booking confirmed

Auto ride, 8 km, fare Rs.110, paying by Card
Charged Rs.110 to card ending 4821
Booking confirmed

Cab ride, 12 km, fare Rs.230, paying by Wallet
Wallet balance Rs.150 is not enough for Rs.230
Booking failed: payment declined

Why This Design Works

There are no type checks anywhere in book(). No if (ride instanceof CabRide), no if (payment instanceof Wallet). Each object handles its own behavior.

It is open for extension. Adding EvCabRide or NetBankingPayment means writing one new class. RideBookingService does not change, so it does not need re-testing either. Three rides and three payment methods already give nine combinations, and none of them needed extra code.

It is also easy to test. In a unit test you can pass in a fake PaymentMethod that always fails and check that the booking is refused, without going anywhere near a real payment gateway.

7. How to Find the Right Abstraction

In an LLD interview, these steps help you decide what deserves an abstraction:

  1. Find what varies. Listen for "different types of", "multiple" or "may be added later" in the problem: ride types, payment methods, notification channels, pricing rules.
  2. Name it after its role, not its implementation. PaymentMethod, not UpiHelper. Notifier, not SmsSender.
  3. Keep the contract small. Put in only what every implementation can honestly support; two or three methods is usually plenty.
  4. Make callers depend on the abstraction. Parameters, fields and return types should say Ride and PaymentMethod, not the concrete classes.
  5. Leave alone what does not vary. If there is one implementation and no realistic second one, a plain class is simpler.

8. Common Mistakes

  • Abstracting everything "just in case". An interface for every class adds files and indirection and buys nothing. Abstract where the variation is real or clearly on its way.
  • Leaky contracts. A method like PaymentMethod.getUpiPin() ties every caller to UPI again.
  • Fat interfaces. If an interface has ten methods and most implementations throw "not supported", split it into smaller ones (the Interface Segregation Principle).
  • Type checks on an abstraction. if (ride instanceof CabRide) in the caller means the abstraction is missing a method. Move that behavior into the classes.
  • Treating it as the same thing as encapsulation. Private fields hide data, but they do not give callers a simpler view on their own. You usually need both.

Summary

Abstraction shows what an object can do and hides how it does it. In Java you reach for an abstract class when related classes share code, an interface when unrelated classes share a capability, and a clean public API when you just want to hide internal steps.

Because callers depend on the abstraction rather than the concrete classes, you can add new types without touching existing code. Abstraction simplifies the outside view and encapsulation protects the inside, and good designs use both. Abstract what really varies, keep the contracts small, and be suspicious of any type check on an abstraction.

Next up is inheritance: how one class can reuse and extend another.