Abstraction in Java

Naveen AnamaneniUpdated · 5 min read

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

Callers see the contract; implementations stay hidden
Checkout.java▶ Try it
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);
    }
}
Output
Navika: Paid Rs. 499 by UPI
Srihitha: Paid Rs. 1250 by card
Advitha: Paid Rs. 3000 by net banking

How it works

  1. PaymentMethod says what a payment method can do: pay(amount). It has no code for how.
  2. Each class supplies its own how.
  3. checkout depends only on PaymentMethod. It doesn't know, or care, which class it's given. Adding a wallet payment later means writing one new class; checkout stays untouched.

Try it yourself: add a WalletPayment class and pay for Navika's second order with it.

Two Tools for Abstraction

ToolWhat it givesCovered in
Abstract classA partly built class: some methods with code, some abstract methods left for subclasses, plus fields and constructors"Abstract Classes and Methods"
InterfaceA 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:

AbstractionEncapsulation
HidesComplexity: how the work is doneData: the object's internal state
Question it answersWhat does this object do?Who may touch this data?
Main toolsInterfaces, abstract classesprivate fields, getters and setters
LevelDesign: deciding the contractImplementation: 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

  1. Easy: Create an interface Shape with double area(), and Circle and Rectangle classes that implement it. Print both areas through Shape variables.
  2. Harder: Write static long totalBill(PaymentMethod method, long[] items) that adds the items and pays the total. Call it with two different payment methods.

Quick check: Abstraction

4 questions
1Abstraction mainly hides:
2Which two tools give abstraction in Java?
3checkout(String, PaymentMethod, long) can accept:
4Encapsulation, unlike abstraction, mainly hides:

Key Takeaways

Was this tutorial helpful?

Lesson 132 of 482