Abstraction in Java
At a shop counter you pay by scanning a UPI code, tapping a card or using net banking. You don't need to know how the bank's servers talk to each other; you only need "pay this amount". Abstraction is that idea in code: expose what an object can do, and hide how it does it. Code that uses the object depends only on the "what", so the "how" can change, or be swapped for a completely different one, without breaking anything.
What, Not How
public class Checkout {
interface PaymentMethod {
String pay(long amount);
}
static class UpiPayment implements PaymentMethod {
public String pay(long amount) {
return "Paid Rs. " + amount + " by UPI";
}
}
static class CardPayment implements PaymentMethod {
public String pay(long amount) {
return "Paid Rs. " + amount + " by card";
}
}
static class NetBanking implements PaymentMethod {
public String pay(long amount) {
return "Paid Rs. " + amount + " by net banking";
}
}
static void checkout(String customer, PaymentMethod method, long amount) {
System.out.println(customer + ": " + method.pay(amount));
}
public static void main(String[] args) {
checkout("Navika", new UpiPayment(), 499);
checkout("Srihitha", new CardPayment(), 1250);
checkout("Advitha", new NetBanking(), 3000);
}
}Navika: Paid Rs. 499 by UPI
Srihitha: Paid Rs. 1250 by card
Advitha: Paid Rs. 3000 by net bankingHow it works
PaymentMethodsays what a payment method can do:pay(amount). It has no code for how.- Each class supplies its own how.
checkoutdepends only onPaymentMethod. It doesn't know, or care, which class it's given. Adding a wallet payment later means writing one new class;checkoutstays untouched.
Try it yourself: add a WalletPayment class and pay for Navika's second order with it.
Two Tools for Abstraction
| Tool | What it gives | Covered in |
|---|---|---|
| Abstract class | A partly built class: some methods with code, some abstract methods left for subclasses, plus fields and constructors | "Abstract Classes and Methods" |
| Interface | A pure contract: method signatures that any class can promise to provide, plus optional default methods | "Interfaces in Java" |
Abstraction also exists without either: every well-named method is a small abstraction. Math.sqrt(2) hides the algorithm completely.
Abstraction vs Encapsulation
The two are often confused, because both "hide" something:
| Abstraction | Encapsulation | |
|---|---|---|
| Hides | Complexity: how the work is done | Data: the object's internal state |
| Question it answers | What does this object do? | Who may touch this data? |
| Main tools | Interfaces, abstract classes | private fields, getters and setters |
| Level | Design: deciding the contract | Implementation: protecting the details |
They work together: an interface (abstraction) is implemented by a class whose fields are private (encapsulation, see "Encapsulation in Java").
Common Mistakes
Designing the interface around one implementation. A PaymentMethod with getUpiId() isn't abstract; card payments have no UPI id.
Abstracting too early. One implementation with no second in sight rarely needs an interface yet.
Mixing up abstraction and encapsulation in interviews. Abstraction hides complexity; encapsulation hides data.
Interview Corner
What is abstraction in Java?
Abstraction is exposing only the essential behavior of an object, what it does, while hiding the implementation details. Java achieves it with abstract classes, which can mix implemented and abstract methods, and interfaces, which define contracts any class can implement. Code written against the abstract type works with any implementation, so implementations can change or be added without affecting callers.
What is the difference between abstraction and encapsulation?
Abstraction hides complexity at the design level by exposing a simple contract, using interfaces and abstract classes. Encapsulation hides data at the implementation level by keeping fields private and controlling access through methods. Abstraction answers "what does it do"; encapsulation answers "who can change its state".
Practice
- Easy: Create an interface
Shapewithdouble area(), andCircleandRectangleclasses that implement it. Print both areas throughShapevariables. - Harder: Write
static long totalBill(PaymentMethod method, long[] items)that adds the items and pays the total. Call it with two different payment methods.
Key Takeaways
Was this tutorial helpful?