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.