What is the difference between abstraction and encapsulation?
Abstraction and encapsulation are two key concepts in object-oriented programming (OOP), but they serve different purposes. Here's a breakdown of their differences and how they complement each other.
Key Differences Between Abstraction and Encapsulation
| Feature | Abstraction | Encapsulation |
|---|---|---|
| Definition | Hiding the implementation details while showing only the essential features. | Hiding the internal state and requiring all interactions to happen through controlled methods (data hiding). |
| Purpose | Focuses on what an object does (behavior/interface). | Focuses on how the object achieves functionality (implementation and access control). |
| Focus | Reduces complexity by exposing only relevant information. | Protects data integrity by preventing unauthorized access or modification. |
| Implementation | Achieved using abstract classes, interfaces, or methods. | Achieved using access modifiers (private, protected, public). |
| Use Case | Design-level concept to define functionality without implementation. | Implementation-level concept to secure and control access to data and methods. |
| Example Question | "What functionalities should a car have (e.g., drive, brake)?" | "How should the car's engine or braking system be implemented internally?" |
Detailed Explanation
1. Abstraction
- What It Does: Abstraction focuses on hiding the internal implementation details and exposing only the functionality or behavior to the user.
- How It Helps: It allows developers to work at a higher level without worrying about complex internal mechanics.
- Key Features:
- Achieved using abstract classes, interfaces, or abstract methods.
- Helps to define a contract for functionality.
Example: In a car:
- Abstraction is deciding that a car should have functions like
drive()andbrake(). - The user knows these functions exist but doesn't care about how they are implemented internally.
In Java:
abstract class Vehicle { abstract void drive(); // Abstract method: No implementation abstract void brake(); } class Car extends Vehicle { @Override void drive() { System.out.println("Car is driving"); } @Override void brake() { System.out.println("Car is braking"); } }
2. Encapsulation
- What It Does: Encapsulation focuses on restricting direct access to the internal state of an object and providing controlled access through methods or properties.
- How It Helps: It secures the data and prevents unintended interference or misuse.
- Key Features:
- Achieved using access modifiers like
private,protected, orpublic. - Enforces data hiding by allowing access only through getters and setters.
- Achieved using access modifiers like
Example: In a car:
- Encapsulation involves making the
enginevariable private and providing methods likestartEngine()orstopEngine()to control access.
In Java:
class Car { private String engineState; // Private variable public void startEngine() { engineState = "ON"; System.out.println("Engine started"); } public void stopEngine() { engineState = "OFF"; System.out.println("Engine stopped"); } public String getEngineState() { return engineState; } }
How They Complement Each Other
- Abstraction defines the required functionalities, leaving the implementation to the specific class or module.
- Encapsulation ensures that the implementation details remain secure and accessible only through controlled interfaces.
Together, they enhance the modularity, readability, and security of object-oriented systems.
For mastering object-oriented design principles, explore Grokking System Design Fundamentals or Grokking Advanced Coding Patterns for Interviews on DesignGurus.io. These resources provide practical insights into abstraction, encapsulation, and their applications in real-world scenarios.
TAGS
Coding Interview
CONTRIBUTOR

Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What Is the Citadel Interview Process Like? (Round by Round)
Citadel's engineering loop explained: the brutal HackerRank assessment, algorithmic phone screens, low-latency system design, team-fit rounds, and the hiring committee.
What to expect in a 30 minute coding interview?
What is asked in an Oracle interview?
What Is the Robinhood Interview Process Like? (Round by Round)
Robinhood's loop: a proctored CodeSignal assessment, finance-flavored coding, domain low-level design (order books, ledgers), system design, and mission-aligned behavioral rounds.
What is the question and answer on Zoom?
What is mock interview session?
Related Courses
New

Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
$123

Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
$197

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
$72
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.