← Back to OOP
OOP

Encapsulation vs Abstraction Interview Questions

What is encapsulation, and how is it different from abstraction? — spoken sample answer for Indian interviews.

  • 5Questions with answers
  • 3Difficulty levels

Questions (5)

Browse beginner, intermediate, and advanced questions with answers — hide them when you want to self-test.

Question 1
Interview Beginner
Question

What is encapsulation, and how is it different from abstraction?

Answer:

Encapsulation is bundling data with the methods that change it, and hiding the fields. Abstraction is hiding how the work is done behind a simpler interface. They are related, not the same word. BankAccount with a private balance and deposit/withdraw is encapsulation — I protect an invariant. The caller not knowing whether transactions sit in SQL or in memory is abstraction. I can have encapsulation without a rich abstraction, and I can have an abstract API that still leaks fields. Campus interviews like this distinction, so I always give both examples.

Question 2
Interview Intermediate
Question

Show a leaky abstraction.

Answer:

A repository that returns a JDBC ResultSet, or a cache API that forces callers to catch RedisConnectionException. I would wrap that in a DTO and a domain exception. A public getter that returns the live internal list is an encapsulation leak — I return a copy.

Question 3
Interview Beginner
Question

Should every field have a getter and setter?

Answer:

No. That is a public struct with extra typing. I expose what the domain needs. If age is derived from date of birth I do not set age. If items should only grow through addItem I do not expose the list setter.

Question 4
Interview Advanced
Question

How does encapsulation help concurrency?

Answer:

If mutation lives inside the object I can synchronize in one place. If every caller mutates a public list I cannot reason about threads. I can swap HashMap for ConcurrentHashMap without touching callers.

Question 5
Interview Intermediate
Question

Can you have abstraction without classes?

Answer:

Yes. A function that hides HTTP, or a REST API over a database. OOP classes are one vehicle. I would not say abstraction equals abstract class. In an OOP interview I still show an interface, then I say the idea is broader.

Practice with AI mock interviews

Run OOP mock interviews with AI follow-ups, instant feedback, and analytics on AiLx.

Free to start · No credit card required