Prévia do material em texto
Difference between Abstraction and Encapsulation in Java with
Examples
Last Updated : 4 May, 2026
Abstraction and Encapsulation are two fundamental concepts of Object-Oriented Programming in
Java. Both help in designing secure, maintainable, and flexible applications, but they serve different
purposes. Understanding their difference is important for writing clean and scalable code.
Both are core principles of OOP in Java
Help in improving code security and maintainability
Often used together but solve different problems
Abstraction
Abstraction focuses on hiding implementation details and showing only the essential features of an
object. It allows you to define what an object does without explaining how it does it.
Achieved using abstract classes and interfaces
Hides complex logic from the user
Improves flexibility and reduces code complexity
Real-Life Example: When you drive a car, you just use the steering, brake, and accelerator. You
don’t need to know how the engine works internally.
Output
Car starts with key
Explanation: The Vehicle class declares an abstract method start() without implementation and Car
class provides the actual implementation of the start() method. User only calls start() without
knowing how it works internally (implementation is hidden).
abstract class Vehicle {
abstract void start(); // abstract method
}
class Car extends Vehicle {
void start() {
System.out.println("Car starts with key");
}
}
public class Geeks{
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
Java Tutorial Advanced Java Interview Questions Exercises Examples Quizzes Projects Cheatsheet DSA in Java Java Collection
Sign In
Courses
Tutorials
Interview Prep
06/08/2026, 10:49 Difference between Abstraction and Encapsulation in Java with Examples - GeeksforGeeks
https://www.geeksforgeeks.org/java/difference-between-abstraction-and-encapsulation-in-java-with-examples/ 1/3
https://www.geeksforgeeks.org/java/object-oriented-programming-oops-concept-in-java/
https://www.geeksforgeeks.org/java/abstraction-in-java-2/
https://www.geeksforgeeks.org/java/abstract-classes-in-java/
https://www.geeksforgeeks.org/java/interfaces-in-java/
https://www.geeksforgeeks.org/java/java/
https://www.geeksforgeeks.org/advance-java/advanced-java/
https://www.geeksforgeeks.org/java/java-interview-questions/
https://www.geeksforgeeks.org/java/java-exercises/
https://www.geeksforgeeks.org/java/java-programming-examples/
https://www.geeksforgeeks.org/java/java-quiz/
https://www.geeksforgeeks.org/blogs/java-projects/
https://www.geeksforgeeks.org/java/java-cheat-sheet/
https://www.geeksforgeeks.org/dsa/dsa-in-java/
https://www.geeksforgeeks.org/java/java-collection-tutorial/
https://www.geeksforgeeks.org/
Encapsulation
Encapsulation focuses on wrapping data (variables) and methods into a single unit and restricting
direct access to the data. It protects the internal state of an object.
Achieved using private variables and getter/setter methods
Provides data security and control
Helps in maintaining integrity of data
Real-Life Example: A bank account does not allow direct access to balance. You can only access
or modify it through methods like deposit() or withdraw().
Output
90
Explanation: The variable marks is declared as private, so it cannot be accessed directly. The
getMarks() method is used to read the value, and setMarks() is used to update it. This ensures
controlled access and prevents invalid data from being assigned.
Abstraction vs Encapsulation
Feature Abstraction Encapsulation
Definition
Abstraction is the process of hiding
implementation details and showing only
functionality to the user.
Encapsulation is the process of binding
data and methods together and restricting
direct access to data.
Focus
Abstraction focuses on what an object
does rather than how it does it.
Encapsulation focuses on protecting data
by controlling how it is accessed.
class Student {
private int marks; // private variable
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
if(marks >= 0) {
this.marks = marks;
}
}
}
public class Geeks{
public static void main(String[] args) {
Student s = new Student();
s.setMarks(90);
System.out.println(s.getMarks());
}
}
06/08/2026, 10:49 Difference between Abstraction and Encapsulation in Java with Examples - GeeksforGeeks
https://www.geeksforgeeks.org/java/difference-between-abstraction-and-encapsulation-in-java-with-examples/ 2/3
https://www.geeksforgeeks.org/java/encapsulation-in-java/
Comment GeeksforGeeks
Explore
Basics
OOP & Interface
Collections
Advance Java Concepts
Practice Java
Courses
Feature Abstraction Encapsulation
Goal
Abstraction helps reduce complexity and
improve code maintainability.
Encapsulation helps ensure data security
and maintain data integrity.
Level
Abstraction works at the design or
interface level.
Encapsulation works at the
implementation level.
Implementation
Abstraction is implemented using
abstract classes and interfaces.
Encapsulation is implemented using
access modifiers like private, public, and
protected.
Data Handling
Abstraction exposes only the necessary
features to the user.
Encapsulation hides data and provides
controlled access using getters and
setters.
116
Corporate & Communications Address:
A-143, 6th Floor, Sovereign Corporate
Tower, Sector- 136, Noida, Uttar
Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante
Apartment, Sector 137, Noida, Gautam
Buddh Nagar, Uttar Pradesh, 201305
Company
About Us
Legal
Privacy
Policy
Contact Us
Advertise
with us
GFG
Corporate
Solution
Campus
Training
Program
Explore
POTD
Job-A-
Thon
Blogs
Nation
Skill Up
Tutorials
Programming
Languages
DSA
Web
Technology
AI, ML &
Data Science
DevOps
CS Core
Subjects
Interview
Preparation
Software and
Tools
Courses
ML and Data
Science
DSA and
Placements
Web
Development
Programming
Languages
DevOps &
Cloud
GATE
Trending
Technologies
Videos
DSA
Python
Java
C++
Web
Development
Data Science
CS Subjects
Preparation
Corner
Interview
Corner
Aptitude
Puzzles
GfG 160
System Design
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
06/08/2026, 10:49 Difference between Abstraction and Encapsulation in Java with Examples - GeeksforGeeks
https://www.geeksforgeeks.org/java/difference-between-abstraction-and-encapsulation-in-java-with-examples/ 3/3
https://www.geeksforgeeks.org/user/code_r/
https://www.geeksforgeeks.org/user/code_r/
https://www.geeksforgeeks.org/user/code_r/
https://www.geeksforgeeks.org/
https://in.linkedin.com/company/geeksforgeeks
https://www.instagram.com/geeks_for_geeks/
https://twitter.com/geeksforgeeks
https://www.facebook.com/geeksforgeeks.org/
https://www.youtube.com/geeksforgeeksvideos
https://www.geeksforgeeks.org/about/
https://www.geeksforgeeks.org/legal/
https://www.geeksforgeeks.org/legal/privacy-policy/
https://www.geeksforgeeks.org/about/contact-us/
https://www.geeksforgeeks.org/advertise-with-us/
https://www.geeksforgeeks.org/gfg-corporate-solution/
https://www.geeksforgeeks.org/campus-training-program/
https://www.geeksforgeeks.org/problem-of-the-day
https://practice.geeksforgeeks.org/events/rec/job-a-thon/
https://www.geeksforgeeks.org/category/blogs/?type=recent
https://www.geeksforgeeks.org/nation-skill-up/
https://www.geeksforgeeks.org/computer-science-fundamentals/programming-language-tutorials/
https://www.geeksforgeeks.org/dsa/dsa-tutorial-learn-data-structures-and-algorithms/
https://www.geeksforgeeks.org/web-tech/web-technology/
https://www.geeksforgeeks.org/machine-learning/ai-ml-and-data-science-tutorial-learn-ai-ml-and-data-science/
https://www.geeksforgeeks.org/devops/devops-tutorial/
https://www.geeksforgeeks.org/gate/gate-exam-tutorial/
https://www.geeksforgeeks.org/aptitude/interview-corner/https://www.geeksforgeeks.org/websites-apps/software-and-tools-a-to-z-list/
https://www.geeksforgeeks.org/courses/category/machine-learning-data-science
https://www.geeksforgeeks.org/courses/category/dsa-placements
https://www.geeksforgeeks.org/courses/category/development-testing
https://www.geeksforgeeks.org/courses/category/programming-languages
https://www.geeksforgeeks.org/courses/category/cloud-devops
https://www.geeksforgeeks.org/courses/category/gate
https://www.geeksforgeeks.org/courses/category/trending-technologies/
https://www.geeksforgeeks.org/videos/category/sde-sheet/
https://www.geeksforgeeks.org/videos/category/python/
https://www.geeksforgeeks.org/videos/category/java-w6y5f4/
https://www.geeksforgeeks.org/videos/category/c/
https://www.geeksforgeeks.org/videos/category/web-development/
https://www.geeksforgeeks.org/videos/category/data-science/
https://www.geeksforgeeks.org/videos/category/cs-subjects/
https://www.geeksforgeeks.org/interview-prep/interview-corner/
https://www.geeksforgeeks.org/aptitude/aptitude-questions-and-answers/
https://www.geeksforgeeks.org/aptitude/puzzles/
https://www.geeksforgeeks.org/courses/gfg-160-series
https://www.geeksforgeeks.org/system-design/system-design-tutorial/
https://www.geeksforgeeks.org/
https://www.geeksforgeeks.org/copyright-information/
https://geeksforgeeksapp.page.link/gfg-app
https://geeksforgeeksapp.page.link/gfg-app