What is the difference between abstraction and encapsulation?
What is the difference between abstraction and encapsulation?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
25-Jun-2023Abstraction and encapsulation are two important concepts in object-oriented programming (OOP). They are both ways of hiding information, but they do so in different ways.
Abstraction is the process of hiding implementation details from the user. This means that the user only sees the essential behavior of the object, without having to worry about how it is implemented. For example, a user of a Circle object only needs to know that they can draw the circle and calculate its area. They do not need to know how the circle is implemented, such as the fact that it is made up of a set of points.
Encapsulation is the process of grouping data and methods together into a single unit. This means that the data and methods are protected from outside access. For example, the Circle object might have a property called radius. This property is encapsulated, which means that the user cannot directly access it. Instead, they must use the getRadius() and setRadius() methods to access the property.
The main difference between abstraction and encapsulation is that abstraction hides implementation details, while encapsulation hides data. Abstraction is concerned with what the object does, while encapsulation is concerned with how the object does it.
Here is a table that summarizes the differences between abstraction and encapsulation:
Here are some examples of where abstraction and encapsulation might be used:
Abstraction:
Encapsulation: