Encapsulation in Object-Oriented Programming

Welcome to this post where we will explore another pillar of object-oriented programming: encapsulation and abstraction.

 

 

1 - What is encapsulation in object-oriented programming

We say that encapsulation in object-oriented programming is when we limit or restrict access to a property, granting access only to the elements that need it and not to any others.

The most common aspect of encapsulation is classes, where we encapsulate and group both methods and properties.

Another very common example of encapsulation is the use of getters and setters for properties within a class. By default, they provide the “normal” value, but we can modify them so that the value changes.

private decimal _currentSpeed { get; set; }public decimal CurrentSpeed {    get{        return _currentSpeed + 2;    }    set{        _currentSpeed = value;    }}

In the example we just saw, we have two properties; both refer to the current speed, but there are slight differences.

One is private, so its value cannot be accessed from outside the class.

The second is public and accesses the previously mentioned private property. Why do we do this?

The example we have shown is a real case: cars usually display a couple of kilometers per hour more than the actual speed. So we encapsulate that logic within the setter, which is hidden from the consumer, who would see on their odometer the speed with those two extra kilometers per hour.

We can say that encapsulation is a way of hiding information between entities, showing only the most necessary information to each other.

 

1.1 - Access Modifiers

As we have seen, encapsulation is closely related to access modifiers, which are explained in detail in the post dedicated to access modifiers.(link)

It is worth remembering them anyway.

  • Public: full access
  • Private: access only from within the class or struct that contains them
  • Internal: access from within the same project
  • Protected: from the same class or from classes that inherit from it
  • 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 that derive from it

Note: Interfaces are also important, and we'll see them in the next post.

 

 

2 - Abstraction in object-oriented programming

Note: do not confuse this with abstraction in C# (future post).

Abstraction is a concept very similar to encapsulation, with the main difference being that abstraction allows us to represent the real world in a simpler way. It can also be defined as the method of identifying necessary functionalities without going into detail about what we are doing.

The clearest example is when we brake in a car: for us, the user, it is simply stepping on the brake, but behind the scenes, the car performs a large number of actions. Also, imagine that in all cars, the driver brakes in the same way, regardless of whether the brakes are disc or drum brakes, while the car itself performs different actions.

Stepping on the brake would be the level of abstraction.

 

 

3 - Differences between encapsulation and abstraction

AbstractionEncapsulation
Looks for the solution in designLooks for the solution in implementation
Only relevant informationCode hiding to protect it
Focused on executionFocused on execution

 

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café