Quick revision
The short answer
Abstraction means exposing what an object does while hiding how it does it, so callers work with a contract such as List, InputStream or DataSource and the implementation behind it can be swapped, for example ArrayList for LinkedList. Encapsulation is about one class protecting its own state and invariants by controlling how that state changes. Abstraction shapes the outside view callers depend on; encapsulation guards the inside, and Java supports abstraction mainly through interfaces and abstract classes.
What to remember
In this answer, we saw that abstraction exposes what an object does and hides how, using JDK examples such as List, InputStream and DataSource, and that programming against the abstract type let us swap ArrayList for LinkedList by changing one line. Encapsulation, by contrast, protects one class's internal state and invariants, as ArrayList does with its private array and size. Abstraction shapes the contract callers depend on and encapsulation guards the inside of each implementation, and Java supports abstraction through interfaces for roles and abstract classes for shared implementation.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Abstraction means exposing what an object does while hiding how it does it, so callers work with a contract such as List, InputStream or DataSource and the implementation behind it can be swapped, for example ArrayList for LinkedList. Encapsulation is about one class protecting its own state and invariants by controlling how that state changes. Abstraction shapes the outside view callers depend on; encapsulation guards the inside, and Java supports abstraction mainly through interfaces and abstract classes.
Why this question is asked
Abstraction and encapsulation are often explained together, and many answers blur them into 'hiding details'. That makes it hard to use either well: we end up with interfaces that leak implementation details, or classes with private fields that still let callers break them. Interviewers ask for the difference with a JDK example to see whether we can explain each idea precisely and recognise it in code we use every day. In this answer, we will define abstraction, look at three JDK abstractions, see how swapping implementations works, define encapsulation in contrast, and compare the two side by side.
1A Quick Recap: What Abstraction Means
Abstraction is the idea of describing an object by what it does, its operations and their meaning, and leaving out how it does them. The description is a contract: a set of method signatures plus documented behaviour. Code that uses the object, the caller, depends only on that contract. Everything below it, the data structures, algorithms, network calls and file handles, can change without the caller noticing.
In Java the main tools are interfaces, which describe operations without any state, and abstract classes, which can describe operations and also share some implementation. A good abstraction is small, named after its role, and documented precisely enough that a caller never has to look inside an implementation to use it correctly.
2Three Abstractions From the JDK
List is the easiest example. It says what a list does: keep elements in order, allow duplicates, get and set by index, add and remove. It says nothing about arrays or linked nodes. InputStream is a second example: it offers read(), which returns the next byte or -1 at the end, and close(). Whether the bytes come from a file, a socket, a zip entry or a byte array is hidden. DataSource, from javax.sql, is a third: getConnection() gives a JDBC connection, and the caller does not know whether it was newly opened, taken from a HikariCP pool or provided by an application server.
Notice that countBytes was written once and works with sources that did not even exist when it was written. That is the practical value of abstraction: the caller is decoupled from the implementation. (In real code we would wrap the stream in a BufferedInputStream or use in.transferTo for speed, which is another implementation detail the abstraction lets us change.)
3Programming Against the Abstraction
To get that benefit, callers must declare variables, parameters and return types with the abstract type, not the concrete class. Then swapping the implementation is a one-line change at the place where the object is created.
The results are identical because both classes honour the List contract. What changes is cost: get(i) is constant time on ArrayList and linear on LinkedList, and adding at the front is the reverse. Choosing between them is an implementation decision that callers are protected from. Spring applies the same idea at a larger scale: a repository or a service depends on a DataSource or a PaymentGateway interface, and configuration decides which implementation is injected.
4Encapsulation, in Contrast
Encapsulation answers a different question: who is allowed to change this object's state, and how? A class encapsulates its state when its fields are private and every public method keeps the object valid, keeping its invariants, the rules that must always hold. ArrayList is a good example from the inside: it keeps an elementData array and a size field, both private, and every method updates them consistently. No caller can set size to 100 while the array holds 3 elements.
So the two ideas sit at different levels. Abstraction is about the contract that callers see, and it often spans many classes: List has dozens of implementations. Encapsulation is about the internals of one class and protects its state, whatever contract it implements. They work together: the List abstraction lets us swap ArrayList for LinkedList, and each class's encapsulation makes sure its own internals stay correct.
| Aspect | Abstraction | Encapsulation |
|---|---|---|
| Question it answers | What can callers do with this? | Who may change the state, and how? |
| Focus | the contract callers depend on | the internals of one class |
| Java tools | interfaces, abstract classes | private fields, validating methods |
| JDK example | List, InputStream, DataSource | ArrayList's private elementData and size |
| Benefit | implementations can be swapped | invariants always hold |
| Typical failure | callers depend on a concrete class | setters or getters leak mutable state |
5Interfaces or Abstract Classes?
Both express abstraction, but they fit different needs. An interface describes a role and has no instance state, so a class can implement many of them, and default methods let it offer small conveniences without forcing them on implementations. An abstract class can hold fields and constructors and share real implementation, but a class can extend only one. The JDK uses both together: List is the interface callers use, and AbstractList is an abstract class that implementers can extend to get most methods for free by implementing only get and size. As a rule, callers should depend on the interface, and abstract classes are a convenience for implementers.
6Follow-up questions
Can a class be well encapsulated but a poor abstraction?
Yes. A class can keep its fields private and its invariants safe while its public methods expose implementation details, for example a CustomerCache whose methods are named after Redis commands. Callers then depend on Redis even though they cannot touch the internals. Good design needs both: a contract named after the role, and protected internals behind it.
Is an abstraction that leaks still useful?
Every abstraction leaks a little; performance is the usual leak, as with get(i) on a LinkedList. The abstraction is still useful as long as the leaks are documented and rarely matter. When callers keep needing to know which implementation they have, the abstraction is in the wrong place and should be redesigned.
7Conclusion
In this answer, we saw that abstraction exposes what an object does and hides how, using JDK examples such as List, InputStream and DataSource, and that programming against the abstract type let us swap ArrayList for LinkedList by changing one line. Encapsulation, by contrast, protects one class's internal state and invariants, as ArrayList does with its private array and size. Abstraction shapes the contract callers depend on and encapsulation guards the inside of each implementation, and Java supports abstraction through interfaces for roles and abstract classes for shared implementation.
Open with
Abstraction means exposing what something does and hiding how it does it, so callers depend on a contract rather than an implementation. The JDK is full of examples.
Then, if they want more
List says you can add, get by index and remove, but not whether it is an array or linked nodes. InputStream says read gives you the next byte or minus one at the end, without saying whether the bytes come from a file, a socket or memory.
DataSource gives you a connection without telling you whether it came from a pool. Because callers program against these types, implementations can be swapped: if I declare List names and create an ArrayList, I can change it to a LinkedList in one line and every call still works, only the performance profile changes. Encapsulation is a different idea.
It is about a single class protecting its own state and invariants, by keeping fields private and only allowing changes through methods that keep the object valid. ArrayList encapsulates its internal array and size so no caller can make them inconsistent.
So abstraction is about the outside view, the contract, often shared by many classes, and encapsulation is about the inside of one class. In Java we express abstraction mainly with interfaces, and with abstract classes when we also want to share some implementation.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain abstraction with an example from the JDK, and how it differs from encapsulation.": https://scrivio-interview-library.netlify.app/java/oop-principles/2e25f619c3/