Interview Q&A / Java

BeginnerQuick answer1 min readUpdated 3 Oct 2026

What is the difference between encapsulation and abstraction in Java?

SHORT ANSWER

Abstraction hides complexity by showing only what a user needs, usually through interfaces or abstract classes. Encapsulation hides data by keeping fields private and exposing controlled methods. One is about design, the other about protection.

Explain it simply

A scooter has a throttle and brake (abstraction: you do not need to know the engine). The fuel tank is sealed and you cannot pour petrol straight into the carburettor (encapsulation: access is controlled). Abstraction answers "what can I do?". Encapsulation answers "who may touch the inside?".

Example

java
interface UpiPayment {                     // abstraction: only the what
    boolean pay(String toUpiId, double amount);
}
class PhonePeStyle implements UpiPayment {
    private double walletBalance = 500;    // encapsulation: hidden state

    public boolean pay(String toUpiId, double amount) {
        if (amount <= 0 || amount > walletBalance) return false;
        walletBalance -= amount;           // only changed through rules
        return true;
    }
}

What to say in the interview

"Abstraction is achieved with interfaces and abstract classes, and it lets callers depend on a contract rather than an implementation. Encapsulation is achieved with private fields and public methods, and it protects the object's invariants, for example a balance can never go negative. They work together, but abstraction hides complexity at the design level while encapsulation hides data at the implementation level."

Common mistakes

  • Giving the same definition for both.
  • Writing a private field with a public setter that does no checking, which defeats encapsulation.

#java#oop#design

Related questions

Practise more Java questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Java quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.