Table of contents
1.
Introduction
2.
What is Abstraction?
3.
What is Encapsulation?
4.
Difference between Abstraction and Encapsulation
5.
Frequently Asked Questions
5.1.
What is object encapsulation?
5.2.
What is abstraction and what are its different types?
5.3.
What are the types of encapsulation?
6.
Conclusion
Last Updated: Sep 9, 2024
Easy

Difference between Abstraction and Encapsulation

Author Mehak Goel
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Object-oriented programming is a programming paradigm based on the concept of “objects” (in Java, everything is an Object) that contain data in the form of fields that are commonly referred to as attributes; and code, in the form of procedures, usually referred to as methods.

These two topics come under OOPs (OOP – Object Oriented Programming). Inside Object-Oriented Programming, we learn how to build codes in which we can store both methods and data .Explore the difference between Abstraction and Encapsulation or abstraction vs encapsulation.

Before moving on to the question, let’s revise what these terms actually mean and the utilization of Abstraction and Encapsulation.

Also see, Duck Number in Java and Hashcode Method in Java

What is Abstraction?

The literal meaning of the word abstract is things that summarize or concentrate the essentials of a bigger thing or several things.

In the Abstraction process, we try and obtain an abstract view, model, or structure of a true-life problem and reduce its unnecessary details. With definition of properties of problems, including the info which are affected and therefore the operations which are identified, the model abstracted from problems may be a customary solution to the present kind of problems. It's an efficient way since there are nebulous real-life problems that have similar properties.

In simple terms, it's hiding the unnecessary details & showing only the essential parts/functionalities to the user.

 

Let us understand abstraction with a real-world example.

You have all the info of citizens of a country like their name, age, gender, favorite movie, favorite food, etc. You're asked to arrange a chart mentioning the total number of females, males, age, and full name. During this case, you merely need three data, and the rest all the information doesn't seem to be of any use. So, this process of choosing useful data and hiding unwanted data is abstraction.

 

Let us understand this idea via code:

//abstract class
abstract class Car {
    abstract void accelerate();
}
//concrete class
class Suzuki extends Car {
    void accelerate() {
        System.out.println("Honda::retard");

    }
}
class Main {
    public static void main(String args[]) {
        Car obj = new Suzuki(); //Car object =>contents of Suzuki
        obj.accelerate(); //call the method  
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Honda::retard


Try this code by yourself on Online Java Compiler.

What is Encapsulation?


Encapsulation is a method of making a complex system easier to handle for end users. Encapsulation is the process of mixing data and functions into one single unit called class. In Encapsulation, one cannot access the information directly; it can only be accessed using the functions present within the class. In simple words, attributes of the class are kept private, and public getter and setter methods are provided to govern these attributes. Encapsulation makes the concept of knowledge hiding possible.

In simple terms, encapsulation offers a protective shield around the data, and therefore, the code which doesn't allow random access of code outside the shield

(Data hiding: a language feature to limit access to members of an object, reducing the negative effect because of dependencies. e.g., "protected", "private" feature in Java).

 

Let us understand encapsulation with a real-world example.

A bag could be a real-time example of encapsulation. The bag encapsulates your book, pen, etc, and you'll be able to only access any stuff inside it.


A capsule is another example of encapsulation. Fundamentally, a capsule encapsulates several combinations of medicine.If medicine combinations are variables and methods, the capsule will act as a class, and the entire process will be referred to as encapsulation, as seen in the diagram below.

Let us see encapsulation in code:

/* File name : EncapTest.java */
public class EncapTest {
    private String name;
    private String idNum;
    private int age;

    public int getAge() {
        return age;
    }
    public String getName() {
        return name;
    }
    public String getIdNum() {
        return idNum;
    }

    public void setAge(int newAge) {
        age = newAge;
    }

    public void setName(String newName) {
        name = newName;
    }

    public void setIdNum(String newId) {
        idNum = newId;
    }
}
/* File name : RunEncap.java */
public class RunEncap {

    public static void main(String args[]) {
        EncapTest encap = new EncapTest();
        encap.setName("Aryan");
        encap.setAge(21);
        encap.setIdNum("12343ms");

        System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

Name : Aryan : 21


You can compile java code just by clicking here.

Know What is Object in OOPs here in detail.

Difference between Abstraction and Encapsulation

AbstractionEncapsulation
  • Abstraction can be defined as a process within which we gain information.

 

  • Encapsulation can be defined as a process within which we encapsulate the info.

 

  • In abstraction, we hide the data which isn't necessary or required by the user.

 

  • In encapsulation, we store or hide all the information in a single unit and we also make a way that prohibits any random code from accessing data from inside. It helps in protecting data.
  • Implementation of abstraction can be done by abstract class or interface.

 

  • Implementation of encapsulation can be done by using access modifiers like private, protected, and public.
  • In the abstraction method, the implementation complexities are usually hidden with the assistance of abstract class and interface.

 

  • In the process of encapsulation, we hide the info using methods such as setters and getters.
  • The objects which help us in the process of abstraction are usually encapsulated.
  • In encapsulation, the performing encapsulation item could also be abstracted or might not be.
  • Problems in abstraction are solved at the interface level.
  • Problems in encapsulation are solved at the implementation level.

Also see,  Swap Function in Java

Frequently Asked Questions

 

What is object encapsulation?


Encapsulation (or OOP Encapsulation) in object-oriented programming (OOP) languages refers to grouping data and its functions that operates on that data into a single unit. Encapsulation is commonly used in several programming languages in the form of classes.
 

What is abstraction and what are its different types?


There are two forms of abstraction: data abstraction and control abstraction. 
Data abstraction refers to the hiding of data specifics, whereas control abstraction refers to the masking of implementation details. Both data and functions may be abstracted using an object-oriented approach.
 

What are the types of encapsulation?


There are three types of Encapsulation.

  • Member Variable Encapsulation.
  • Function Encapsulation.
  • Class Encapsulation.

Conclusion

In this blog, we have discussed the difference between Abstraction and Encapsulation with example. Along with a few more important terminologies with its code.

Recommended Readings:

We hope that this blog has helped you enhance your knowledge regarding throw vs throws in software engineering and if you would like to learn more, check out our articles on java Oops. Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass