390 points
19 13 12

Abstraction, Encapsulation, Polymorphism, and Inheritance are four building blocks of Object-Oriented programming and without these bases, you cannot achieve object-oriented functionality. In this article, we are going to discuss abstraction and encapsulation in detail with examples:


Abstraction refers to hiding unnecessary information from the user and showing only the necessary detail to the user. The concept of abstraction is to hide the complexity of the system from the user. For example, the user sees a car as a single entity, and the individual parts of a car are hidden from the user. Another example could be a cell phone. You only know that what buttons to press and where to touch to perform a specific function but you do not know the internal mechanism of your cell phone. This is abstraction. In programming, the abstraction is achieved by the interfaces or the abstract classes. The difference between both of these is that abstract classes can have concrete methods whereas the interfaces can not have concrete methods. 


Encapsulation refers to enclosing the data and functions into a single unit. This unit is called a class. Encapsulation is achieved by access specifiers i.e. public, private, protected, etc. The data in a class is mostly private and the methods/functions are public. Data cannot be accessed directly outside the class so, we use a method to access the data part of a class. Encapsulation has made the concept of data hiding possible. The main reason for hiding the data is to prevent accidental or intentional changes that could be made either by a programmer or some other person. 

More Posts

Abstraction vs encapsulation in java with example Hafsa_Rao - Aug 1
Abstraction and encapsulation in java with realtime example. Tania - Aug 9
Difference between Abstraction and Encapsulation in C# with example mhfaisal - May 7
Difference between abstraction and encapsulation in c# with a real-time example. Farkhanda Athar - Sep 1, 2020
what is the difference between encapsulation and abstraction in java kesav.eee - Jun 1
Encapsulation is understanding an object and representing it in terms of its characteristics and ________? sakshi - Sep 7
What is encapsulation in C++? Lalit Kumar - Nov 18, 2020
What is the difference between abstraction and encapsulation in java Sanjana Sagar - May 20
Real time example of abstraction in Java mhfaisal - Jul 14
Encapsulation in C++ Yogesh Sharma - Aug 1