Abstraction vs Encapsulation: Difference Between Abstraction and Encapsulation


Every programmer thrives to develop a neat and clean code, and he must use a proper weapon from armour or OOP concepts. Using proper OOP concepts will reduce the complexity and the entity of the objects are dissociated with other parts of the code. Also, it improves readability and can be easily modified if required.

Also, the OOP concept like inheritance provides the extensibility of code. These concepts are helpful for defining abstract classes, updating previously implemented codes, implementing an application for real-life scenarios.

Abstraction, encapsulation is the fundamental concepts in object-oriented programming. And every programmer implements these concepts for a clean and reusable code. But there are a considerable number of differences between these two concepts, let’s understand them in this article.

At first, let’s have a quick walk through the concepts of abstraction and encapsulation.


Abstraction is a process of hiding unnecessary data and showing only relevant data. Out of an ocean of data, we are only maintaining the transparency of some data to the user. This important concept in object-oriented programming will reduce the complexity of the code and increases the readability.

For example, let’s say we were assigned a task to create an online course enrollment portal, and the data fields available to us are {name, age, current occupation, college name, course name, payment method, sibling’s name, marital status, vehicle number}.

After having a look at the data fields available, we’ll come to understand that some of the fields are not necessary for the course enrollment portal, fields like {sibling’s name, marital status, vehicle number} are not required for course enrollment.

So, we need to be specific and choose only the relevant data. Now the necessary fields are {name, age, current occupation, college name, course name, payment method}, this makes an example of abstraction in OOP because we’ve escorted selected information from whole data.

Now, this new data can also be used for some other applications like course status tracker, course completion record, etc. We can use the same data without any changes.

Many people get often confused about abstraction and abstract class, So are they both related?

An abstract class is different from abstraction, abstract classes/methods are created with an intention to implement them in child class or subclass. Whereas abstraction is simply hiding the data, and showing only the relevant data by using access specifiers like public, protected, private.


Encapsulation is binding the data members with member variables. This will avoid the direct access of variables, because direct access of variables may violate privacy, and hiding of the implementation will not be possible.

We may have gone through some classic methods in class like set and get, where the set method is used to update or allocate a value to a variable and the get method is used to read or retrieve the value of a variable. Here we can directly access the variables using the object of that class, but if we want to make a variable private then we should use these settings and get methods.

The concept is simple, we’ll make the set and get methods public and the variables are private. So the variables can be accessed only through the public methods outside the class since private objects are not accessible outside class but accessible inside class. This concept of binding or bundling variables with methods is called encapsulation.

Let us have a look at a simple java program to understand this better.

class CourseRegistration{
    private String student_name;
    private String course_name;
    public String getName(){ 
        return student_name;
    public void setName(String name){ 
    public String getCourse(){
        return course_name;
    public void setCourse(String course){
public class upGrad
    public static void main(String[] args){
        CourseRegistration c=new CourseRegistration();
        c.setCourse(“Data Science”);

In the above code, we’ve created a class CourseRegistration, where the member variables student name, and course name are private, but we are accessing it in another class using the set name and get name functions. In the main class, we’ve created an object of CourseRegistration type. We are initializing and accessing the member variables using the set and get functions.

Now that we’ve seen the definition and example of abstraction let’s discuss the advantages and differences between abstraction and encapsulation.

Advantages of Abstraction

  • Privacy of data is maintained since only relevant data is visible to the user.
  • Reduces the complexity of code and increases readability.
  • The class which implements the abstraction can be used as a parent class by inheriting them, hence improving reusability, and reducing duplication.

Advantages of Encapsulation

  • It helps in binding the data members with member functions.
  • Improves efficiency and user friendly for error analysis.
  • Provides flexibility to the programmer in controlling the accessibility and transparency of the data.

Difference Between Abstraction and Encapsulation


  • Abstraction is hiding the details and implementation of the code.
  • Encapsulation is hiding the data and controlling the visibility of the code.


  • Abstraction is a design level process.
  • Encapsulation is an implementation level process.

Pivotal Ability

  • Abstraction is concerned about what a class instance can do, instead of the implementation of the class.
  • Encapsulation helps in data binding and control over maintaining the transparency of the data.

Use Case

  • Abstraction is a design level process and it is used to reduce the complexity at the designing stage of a project.
  • Encapsulation is an implementation level process, and it is used to provide privacy and maintain control over the transparency of data at the implementation stage of a project.

How to Implement

  • Abstraction can be achieved using class and interfaces in java.
  • Encapsulation is also implemented using classes and the control over data privacy is obtained by specifying access specifiers like protected, public, private.


We’ve gone through the importance of OOP concepts, understood the definition of abstraction and encapsulation. Walked through the examples of abstraction, and encapsulation. We also had a look over the advantages of using OOP concepts like abstraction and encapsulation. And finally, we’ve gone through the difference between abstraction and encapsulation.

Now that you are aware of the definition and implementation of abstraction, encapsulation, try implementing them in your code and try reducing the complexity of the code which you are going to write from now on!

If you’re interested to learn more about Java, full-stack development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Land on Your Dream Job


Leave a comment

Your email address will not be published.

Accelerate Your Career with upGrad

Our Popular Software Engineering Courses