Welcome to this post where we will explore another pillar of object-oriented programming: encapsulation and abstraction.
Index
1 - What is encapsulation in object-oriented programming
Encapsulation in object-oriented programming refers to limiting or restricting access to a property to only the elements that need it and no one else.
The most common example of encapsulation is classes, where we encapsulate and group both methods and properties.
Another very common example of encapsulation is getters and setters for properties within a class. By default, they give us the "normal" value, but we can modify them to change it.
private decimal _velocidadActual { get; set; }
public decimal VelocidadActual
{
get{
return _velocidadActual + 2;
}
set{
_velocidadActual = value;
}
}
In the example we just saw, there are two properties, both referring to the current speed, but with slight differences.
One is private, so from outside the class, its value cannot be accessed.
The second is public and accesses the previously mentioned private one. Why do we do this?
The example above is a real-world case: cars usually display a few extra kilometers per hour compared to the actual speed. So we encapsulate that logic inside the setter, which is hidden from the consumer who, on their speedometer, sees the speed including the extra two kilometers per hour.
We can say that encapsulation is a way of hiding information between entities, showing only the most necessary information among them.
1.1 - Access Modifiers
As we have seen, encapsulation is tied to access modifiers, which are explained in a dedicated post about access modifiers.(link)
It’s still worth recalling them here.
- Public – full access
- Private – access only from within the class or struct that contains them
- Internal – accessible from within the same project
- Protected – accessible from within the same class or from derived classes
- Protected internal – combines protected and internal, allowing access from the same project or from derived classes
- Private protected – allows access from the current class or from classes derived from it
Note: Interfaces are also important and will be covered in the next post.
2 - Abstraction in object-oriented programming
Note: do not confuse with abstraction in C# (future post).
Abstraction is a concept very similar to encapsulation, with the main difference that abstraction allows us to represent the real world in a simpler way. We could also define it as a way of identifying necessary functionalities without going into the details of what we’re doing.
The clearest example is when we brake a car: for us, the user, it is simply pressing the brake, but behind the scenes, the car performs many actions. Imagine, too, that in every car, drivers brake in the same way, regardless of whether the brakes are disc or drum. The car, however, performs different actions.
Pressing the brake would be the level of abstraction.
3 - Differences between encapsulation and abstraction
Abstraction | Encapsulation |
Seeks solutions in design | Seeks solutions in implementation |
Only relevant information | Code hiding for protection |
Focused on execution | Focused on execution |
If there is any problem you can add a comment bellow or contact me in the website's contact form